public class KeyBindingDispatcher extends Object implements KeyEventDispatcher
The mechanism is the same as for component bindings, that is using a pair
InputMap/ActionMap. The maps can be accessed directly to bind
KeyStrokes and Actions individually. Additionally, the implementation provides
convenience methods
to bind pairs of KeyStroke/Action easily.
Once the dispatcher is added to the KeyboardFocusManager, all KeyEvents will
be served first to the bindings (modulo other dispatchers earlier in the chain),
and consumed if an action is available, enabled and performed.
Usage:
KeyBindingDispatcher d = new KeyBindingDispatcher();
d.bind(myKeyStroke, myAction);
KeyboardFocusManager.getCurrentKeyboardFocusManager()
.addKeyEventDispatcher(d);
| Constructor and Description |
|---|
KeyBindingDispatcher() |
| Modifier and Type | Method and Description |
|---|---|
void |
bind(KeyStroke keyStroke,
Action action)
Binds the keyStroke and action, a key is auto-created.
|
void |
bind(KeyStroke keyStroke,
Action action,
Object key)
Binds the keyStroke and action, using the given key which must not be
null.
|
protected ActionEvent |
createActionEvent(KeyEvent e,
Action action)
Creates and returns an ActionEvent that can be passed into the action's
actionPerformed.
|
protected Object |
createAutoKey()
Creates and returns a unique actionMapKey.
|
boolean |
dispatchKeyEvent(KeyEvent e)
Implemented to process the key binding for the event.
|
Object |
get(KeyStroke keyStroke)
Returns the actionMapKey registerd for the keyStroke.
|
Action |
get(Object key)
Returns the Action registered for key or null if none.
|
protected Action |
getAction(KeyEvent e)
Returns the Action bound to the KeyStroke that corresponds to the
keyEvent, or null if none available.
|
ActionMap |
getActionMap()
Returns the ActionMap used for key bindings, lazily created if not yet
done.
|
InputMap |
getInputMap()
Returns the InputMap used for key bindings, lazily created if not yet
done.
|
protected boolean |
processKeyBinding(KeyEvent e)
Processes the keyBinding for the keyEvent and returns a boolean
indicating whether a bound action was performed.
|
void |
put(KeyStroke keyStroke,
Object actionMapKey)
Registers a binding for
keyStroke to
actionMapKey. |
void |
put(Object key,
Action action)
Registers a binding for
key to action. |
public boolean dispatchKeyEvent(KeyEvent e)
Subclasses may re-implement to return true and end the dispatch here.
dispatchKeyEvent in interface KeyEventDispatchere - the keyEvent to dispatchprotected boolean processKeyBinding(KeyEvent e)
e - the KeyEvent to handleprotected Action getAction(KeyEvent e)
e - the keyEvent to find a binding forprotected ActionEvent createActionEvent(KeyEvent e, Action action)
e - the KeyEvent bound to the actionaction - the bound actionSwingUtilities.notifyAction(Action, KeyStroke, KeyEvent,
Object, int)public void bind(KeyStroke keyStroke, Action action, Object key)
keyStroke - the keyStroke to bindaction - the action to bind.key - the key used for binding, must not be null.public ActionMap getActionMap()
public InputMap getInputMap()
public void bind(KeyStroke keyStroke, Action action)
keyStroke - the keyStroke to bindaction - the action to bind.protected Object createAutoKey()
public void put(KeyStroke keyStroke, Object actionMapKey)
keyStroke to
actionMapKey. Semantics are the same as the corresponding
method in InputMap.keyStroke - the keyStroke to bindkey - the key used for binding.InputMap.put(KeyStroke, Object)public Object get(KeyStroke keyStroke)
keyStroke - the keyStroke to bindpublic void put(Object key, Action action)
key to action.
Semantics are the same as that of the corresponding method in ActionMap.key - the key used for binding.action - the action to bind.ActionMap.put(Object, Action)Copyright © 2019. All rights reserved.