• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
Ranch Hand
Posts: 224
2
  • 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
 
Bartender
Posts: 3323
86
  • 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
Bartender
Posts: 3323
86
  • 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: 224
2
  • 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.
 
I didn't say it. I'm just telling you what this tiny ad said.
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic