This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
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: 6
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: 19654
    
  18

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: 2154
    
    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: 6
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Override Java Component keystrokes
 
Similar Threads
KeyListener Stops Working
capturing all keystroke??
Ctrl+X,Ctrl+C,Ctrl+V not working!
check if ctrl-v was pressed in content editable area?
Can't do multiple selections