Trouble with KeyListeners in 1.4.2

From: Aaron Davies (aaron.davies_at_knox.mil.invalid)
Date: 02/23/04


Date: Mon, 23 Feb 2004 16:44:39 GMT

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
aaron.davies@knox.army.mil