Results 1 to 7 of 7
  1. #1
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default Catching TAB via KeyEvent?

    I thought it'd be simple, but apparently not... Wanted to catch the TAB key in one of my Java programs, and added a KeyListener to do the job. The problem is... it catches every single key other than TAB. I've used both KeyPressed, KeyReleased and KeyTyped, but none of them trigger on TAB. Is there a secret to catching TAB, or is it something very weird on my end? The code (as simple as can be, so not sure you even need it):
    Java Code:
        head.addKeyListener(new KeyAdapter()
        {
          public void keyReleased(KeyEvent ke)
          {
            System.out.println("Key released: "+ke);
          }
          public void keyTyped(KeyEvent ke)
          {
            System.out.println("Key typed: "+ke);
          }
          public void keyPressed(KeyEvent ke)
          {
            System.out.println("Key pressed: "+ke);
          }
        }
        );
    Here's the output for pressing "a":
    Java Code:
    Key pressed: java.awt.event.KeyEvent[KEY_PRESSED,keyCode=65,keyText=A,keyChar='a',keyLocation=KEY_LOCATION_STANDARD,rawCode=65,primaryLevelUnicode=97,scancode=30] on frame0
    Key typed: java.awt.event.KeyEvent[KEY_TYPED,keyCode=0,keyText=Unknown keyCode:0x0,keyChar='a',keyLocation=KEY_LOCATION_UNKNOWN,rawCode=0,primaryLevelUnicode=0,scancode=0] on frame0
    Key released: java.awt.event.KeyEvent[KEY_RELEASED,keyCode=65,keyText=A,keyChar='a',keyLocation=KEY_LOCATION_STANDARD,rawCode=65,primaryLevelUnicode=97,scancode=30] on frame0
    Last edited by Toll; 07-06-2011 at 03:22 AM. Reason: Added output

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default

    Tabbing is probably being grabbed by the JVM to move the focus around.
    Don't know right now how to intercept it for the program.

  3. #3
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    It's a graphical program, so the listener isn't focused on a specific component; it's added directly to the Frame, if that makes a difference.

  4. #4
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    The code (as simple as can be, so not sure you even need it):
    A few lines of code doesn't help us because we don't know the context of how it is being used. In the future post a Short, Self Contained, Correct Example

    Is there a secret to catching TAB,
    Why do you want to do this because tabs are used by the focus subsystem? See How to Use the Focus Subsystem (The Java™ Tutorials > Creating a GUI With JFC/Swing > Using Other Swing Features) for more information on how this works. Maybe you should be using the focus subsystem to solve your problem.

    However, I believe, you should be able to access tab KeyStrokes by using Global Event Listeners « Java Tips Weblog.

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    As I said in the above post, it's a completely graphical program; there are no textfields or buttons or any such components. There's only a single frame that the listener is added to, and the entire GUI is drawn to that frame.

  6. #6
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,236
    Rep Power
    7

    Default

    It still doesn't explain why you are listening for tab KeyStrokes. Anyway you've been given some ideas. You can also look in the Swing tutorial for the section on 'How to Use Key Bindings" which may help.

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    393
    Rep Power
    4

    Default

    The reason I wanted to catch tab is because I wanted the program to react to the tab key getting pressed... Anyway, the KeyEventPostProcessor caught it properly. Thanks!

Similar Threads

  1. Listening KeyEvent from two JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-11-2010, 11:31 PM
  2. How to dispatchEvent(KeyEvent evt) to an applet
    By crikey in forum Java Applets
    Replies: 17
    Last Post: 09-24-2010, 11:43 PM
  3. Help in KeyEvent
    By chyrl in forum AWT / Swing
    Replies: 15
    Last Post: 05-26-2010, 09:12 AM
  4. JOptionPane KeyEvent
    By mine0926 in forum NetBeans
    Replies: 5
    Last Post: 05-05-2010, 03:03 AM
  5. Replies: 11
    Last Post: 12-25-2009, 11:30 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •