NoAltKeyEventPostProcessor Class
- public class NoAltKeyEventPostProcessor
extends Object
implements KeyEventPostProcessor
The NoAltKeyEventPostProcessor class sovles the problem that a default
Swing implementation of a dialog or message box requires the ALT key to
be down in order for any mnemonics to work.
Users accustomed to Windows applications expect that the ALT key is not
necessary unless the focus is in a control that consumes normal keystrokes
(such as a text field).
By registering ourselves with the KeyboardFocusManager, we get told about
all keyboard events (globally). We look at each keyPressed, keyTyped, and
keyReleased event. When we see a set of those go by for the same key where
the pressed and typed events were not consumed by any handlers, we step in.
If in the course of watching we see any keyPressed or keyTyped events that
are consumed by another listener, we reset our state and start over again.
This way, typing in a text field or the like never triggers us.
Note that it is ok if the keyReleased event is consumed, because the
default Swing implementation of a mnemonic is to handle (consume) the
keyPressed and keyReleased events for Alt+[key], as well as just the
keyReleased event for plain [key]. This way, if the user releases Alt
before [key] the mnemonic still works. However, it only works if it first
receives the keyPressed event.
Once we see an un-handled key, we construct new keyPressed and keyReleased
events where we fake that the ALT key is depressed. We resend these
keystrokes to give the mnemonics another chance.
There is one significant hurdle with this approach. If more than one post-
processor is running at the same time, nothing works. Even if the other
post-processors are associated with different components. I'm not sure
why this is. The solution is to uninstall any existing post-processor
when a new one is installed, and then to reinstall it (if it's still valid)
when the current one is uninstalled. This also solves the problem that
some classes do not or can not uninstall us themselves.
-
Hierarchy
-
Object
NoAltKeyEventPostProcessor
-
All Implemented Interfaces
-
KeyEventPostProcessor
public void |
-
install ()
- Register this post processor with the KeyboardFocusManager.
|
public boolean |
-
postProcessKeyEvent (KeyEvent e)
- Process all key events
(after all other key event listeners have had their chance).
|
public void |
-
uninstall ()
- Uninstalls this post-processor.
|
Methods from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
NoAltKeyEventPostProcessor
public NoAltKeyEventPostProcessor(JComponent
comp)
install() Method
public void install()
Register this post processor with the KeyboardFocusManager.
postProcessKeyEvent(KeyEvent) Method
public boolean postProcessKeyEvent(KeyEvent
e)
Process all key events
(after all other key event listeners have had their chance).
uninstall() Method
public void uninstall()
Uninstalls this post-processor.
Reinstalls any other post-processor which we preempted.