Package org.jdesktop.swingx.event
Class KeyBindingDispatcher
java.lang.Object
org.jdesktop.swingx.event.KeyBindingDispatcher
- All Implemented Interfaces:
KeyEventDispatcher
KeyEventDispatcher for application-wide KeyBindings.
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);
- Author:
- Jeanette Winzenburg, Berlin
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
Binds the keyStroke and action, a key is auto-created.void
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
Creates and returns a unique actionMapKey.boolean
Implemented to process the key binding for the event.Returns the Action registered for key or null if none.Returns the actionMapKey registerd for the keyStroke.protected Action
Returns the Action bound to the KeyStroke that corresponds to the keyEvent, or null if none available.Returns the ActionMap used for key bindings, lazily created if not yet done.Returns the InputMap used for key bindings, lazily created if not yet done.protected boolean
Processes the keyBinding for the keyEvent and returns a boolean indicating whether a bound action was performed.void
Registers a binding forkey
toaction
.void
Registers a binding forkeyStroke
toactionMapKey
.
-
Constructor Details
-
KeyBindingDispatcher
public KeyBindingDispatcher()
-
-
Method Details
-
dispatchKeyEvent
Implemented to process the key binding for the event. If a binding is available, the bound action is performed and the event is consumed. Does nothing if the event already is consumed. The returned value is false always to allow other dispatchers to handle the event as wellSubclasses may re-implement to return true and end the dispatch here.
- Specified by:
dispatchKeyEvent
in interfaceKeyEventDispatcher
- Parameters:
e
- the keyEvent to dispatch- Returns:
- unconditionally false
-
processKeyBinding
Processes the keyBinding for the keyEvent and returns a boolean indicating whether a bound action was performed.- Parameters:
e
- the KeyEvent to handle- Returns:
- true if a bound action was available, enabled and performed, false otherwise
-
getAction
Returns the Action bound to the KeyStroke that corresponds to the keyEvent, or null if none available.- Parameters:
e
- the keyEvent to find a binding for- Returns:
- the action bound to the keyStroke that corresponds to the keyEvent or null if not available
-
createActionEvent
Creates and returns an ActionEvent that can be passed into the action's actionPerformed. Similar configuration as SwingUtilities.notifyAction, except ignoring standIn actions.- Parameters:
e
- the KeyEvent bound to the actionaction
- the bound action- Returns:
- the actionEvent that will be passed into the action's actionPerformed.
- See Also:
-
bind
Binds the keyStroke and action, using the given key which must not be null.- Parameters:
keyStroke
- the keyStroke to bindaction
- the action to bind.key
- the key used for binding, must not be null.
-
getActionMap
Returns the ActionMap used for key bindings, lazily created if not yet done.- Returns:
- the ActionMap used for key bindings
-
getInputMap
Returns the InputMap used for key bindings, lazily created if not yet done.- Returns:
- the InputMap used for key bindings.
-
bind
Binds the keyStroke and action, a key is auto-created.- Parameters:
keyStroke
- the keyStroke to bindaction
- the action to bind.
-
createAutoKey
Creates and returns a unique actionMapKey.- Returns:
- a unique key used for binding in input/actionMap.
-
put
Registers a binding forkeyStroke
toactionMapKey
. Semantics are the same as the corresponding method in InputMap.- Parameters:
keyStroke
- the keyStroke to bindactionMapKey
- the key used for binding.- See Also:
-
get
Returns the actionMapKey registerd for the keyStroke.- Parameters:
keyStroke
- the keyStroke to bind- Returns:
- the actionMapKey for the keyStroke or null if unbound.
-
put
Registers a binding forkey
toaction
. Semantics are the same as that of the corresponding method in ActionMap.- Parameters:
key
- the key used for binding.action
- the action to bind.- See Also:
-
get
Returns the Action registered for key or null if none.- Parameters:
key
- the key used for binding.- Returns:
- the action registered for key or null it none available.
-