GeeCON Prague 2014*
The moose likes Swing / AWT / SWT and the fly likes Override Java Component keystrokes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Override Java Component keystrokes" Watch "Override Java Component keystrokes" New topic
Author

Override Java Component keystrokes

Ralf Butler
Greenhorn

Joined: Jan 22, 2010
Posts: 8
Hi there,

I want to make my application responding to keystrokes. But it seems that some keystrokes are swallowed by other components. I use a top level component to capture all keystrokes:

This approach works fine for many keystrokes like CTRL+SHIFT+A or CTRL+SHIFT+B. However, I now want to capture CTRL+SHIFT+RIGHT. Can it be that it is swallowed by a jTree which has the focus? It is for sure not captured by the OS because the KeyListener example (http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html) works fine and captures exactly this keystroke.

Help is much appreciated,
Ralf
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19697
    
  20

Moving to our GUI forum.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2190
    
    7
But it seems that some keystrokes are swallowed by other components

Can it be that it is swallowed by a jTree which has the focus?


Correct, Key Bindings for components with focus take precedence over other bindings. In case you are interested in all the default bindings you can check out Key Bindings.

Global Event Listeners may help with a solution.
Ralf Butler
Greenhorn

Joined: Jan 22, 2010
Posts: 8
Hi Rob,

Very interesting blog entries. So I understand that I can capture keyboard events by using AWTEventListener. But what I still don't get is how to capture a particular keystroke, like the "Left" keystroke for example. In addition the "Left" keystroke should not be forwarded to the Jtree component. Do you have a little example for that?

Thank you,
Ralf
 
 
subject: Override Java Component keystrokes