• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why doesn't a {KeyListener} Notice a Tab Key?

 
Kevin Simonson
Ranch Hand
Posts: 190
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm in the process of building an application that needs to handle clicks on a {JPanel} and also keys pressed. So I wrote the following program:

just to see if I could capture events where the user types keys. It works pretty good; I can enter pretty much any key and get a description of the {char} value returned as well as the integer code of the key. But one thing I noticed is that pressing the tab key has no effect at all. I press the tab key and my program doesn't print out anything. It's like the tab key doesn't even exist. Does anybody know why this is happening? Do all keys have integer codes except the tab key?

Kevin S
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's probably because the tab key is used to move focus between components and so it is being consumed.
 
Tony Docherty
Saloon Keeper
Posts: 3138
72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry pressed submit to early.

Add setFocusTraversalKeysEnabled(false); to the panel constructor to disable focus traversal.
 
Kevin Simonson
Ranch Hand
Posts: 190
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:Sorry pressed submit to early.

Add setFocusTraversalKeysEnabled(false); to the panel constructor to disable focus traversal.

Thanks Tony! I added that line to my program and now I can see that the tab key has integer code 9.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!