• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Adding KeyListener

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic