• Post Reply Bookmark Topic Watch Topic
  • New Topic

Adding KeyListener

 
Eric Hoskland
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOT A SWING QUESTION:
How do I add a "global" Keylistener to my GUI.
My main GUI extends FRAME and then upon creation of my GUI I create a bunch of panels and a message area. How do I add a keylistener that will run whenever a key is hit.
I tried doing things like:
this.addKeyListener(this);
this.addKeyListener(new KeyboardListener);
super.addKeyListener(this);
They all compile fine but the listener never runs. The best I can do is add listeners to buttons or message areas that I know will be selected at certain points in time. But that seems ineffecient. I want a listener to be always active no matter what in my frame has focus.
[ August 11, 2003: Message edited by: Eric Hoskland ]
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't add a global KeyListener... a KeyListener has to be associated with a component. What kind of functionality are you trying to achieve... there is probably another way to do this...
 
Jason Dobies
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try working with the InputMap for the frame's content pane. The condition "WHEN_IN_FOCUSED_WINDOW" will intercept any strokes to any of the components in that panel.
You can also try calling registerKeyboardAction on the content pane under the above condition. I've done that in the past on a JTabbedPane to implement an IntelliJ style alt+left and alt+right switching between tabs, no matter what component currently has focus. Just keep in min JDK 1.4 says this method is "obsolete", though it has not officially deprecated it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!