Trouble with KeyListeners in 1.4.2  
Author Message
Aaron Davies





PostPosted: 2004-2-24 0:45:00 Top

java-programmer, Trouble with KeyListeners in 1.4.2 I'm having trouble using KeyListeners in 1.4.2. I'm writing a graphics
application, and I'd like to change the behavior of some of the tools when
modifier keys are held down. The problem is that I use a JToolBar, which
seems to have grabbed hold of the focus and won't let go. The KeyListener
I've registered with the main JFrame doesn't pick up any events (unless I
comment out the JToolBar).

I found a fix on the boards at java.sun.com which involves registering a
KeyEventDispatcher with the current KeyboardFocusManager, and that does
allow my KeyListener to work, but any *other* KeyEvents fail to propagate
further--i.e., alt-F4 no longer quits the app, the alt-keys for my menu
mnemonics stop working, etc.

What do I have to do to get both shift-key detection and other key commands
working at the same time? I understand there were some changes to the focus
system in 1.4.x that cause these problems, but there doesn't seem to be any
documentation on how to work around them.

Any help is much appreciated.

BTW, here's the code for the KeyEventDispatcher, in case it helps:

DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDisp
atcher(

new KeyEventDispatcher() {

public boolean dispatchKeyEvent(final KeyEvent e) {

processKeyEvent(e);

return true;

} // dispatchKeyEvent

} // KeyEventDispatcher constructor

);

--
Aaron Davies
email***@***.com


 
Kleopatra





PostPosted: 2004-2-24 19:49:00 Top

java-programmer >> Trouble with KeyListeners in 1.4.2

Aaron Davies wrote:

> I found a fix on the boards at java.sun.com which involves registering a
> KeyEventDispatcher with the current KeyboardFocusManager, and that does
> allow my KeyListener to work, but any *other* KeyEvents fail to propagate
> further--i.e., alt-F4 no longer quits the app, the alt-keys for my menu
> mnemonics stop working, etc.

no wonder because...

>
> DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDisp
> atcher(
>
> new KeyEventDispatcher() {
>
> public boolean dispatchKeyEvent(final KeyEvent e) {
>
> processKeyEvent(e);
>
> return true;

... unconditionally returning true effectively blocks all further
processing (see api doc).

Not sure if that's the whole story though.

Greetings
Jeanette