org.jdesktop.swingx.event
Class KeyBindingDispatcher
- java.lang.Object
-
- org.jdesktop.swingx.event.KeyBindingDispatcher
-
- All Implemented Interfaces:
- java.awt.KeyEventDispatcher
public class KeyBindingDispatcher extends java.lang.Object implements java.awt.KeyEventDispatcherKeyEventDispatcher 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 ofKeyStroke/Actioneasily. 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 Summary
Constructors Constructor and Description KeyBindingDispatcher()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method and Description voidbind(javax.swing.KeyStroke keyStroke, javax.swing.Action action)Binds the keyStroke and action, a key is auto-created.voidbind(javax.swing.KeyStroke keyStroke, javax.swing.Action action, java.lang.Object key)Binds the keyStroke and action, using the given key which must not be null.booleandispatchKeyEvent(java.awt.event.KeyEvent e)Implemented to process the key binding for the event.java.lang.Objectget(javax.swing.KeyStroke keyStroke)Returns the actionMapKey registerd for the keyStroke.javax.swing.Actionget(java.lang.Object key)Returns the Action registered for key or null if none.javax.swing.ActionMapgetActionMap()Returns the ActionMap used for key bindings, lazily created if not yet done.javax.swing.InputMapgetInputMap()Returns the InputMap used for key bindings, lazily created if not yet done.voidput(javax.swing.KeyStroke keyStroke, java.lang.Object actionMapKey)Registers a binding forkeyStroketoactionMapKey.voidput(java.lang.Object key, javax.swing.Action action)Registers a binding forkeytoaction.
-
-
-
Method Detail
-
dispatchKeyEvent
public boolean dispatchKeyEvent(java.awt.event.KeyEvent e)
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:
dispatchKeyEventin interfacejava.awt.KeyEventDispatcher- Parameters:
e- the keyEvent to dispatch- Returns:
- unconditionally false
-
bind
public void bind(javax.swing.KeyStroke keyStroke, javax.swing.Action action, java.lang.Object key)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
public javax.swing.ActionMap getActionMap()
Returns the ActionMap used for key bindings, lazily created if not yet done.- Returns:
- the ActionMap used for key bindings
-
getInputMap
public javax.swing.InputMap getInputMap()
Returns the InputMap used for key bindings, lazily created if not yet done.- Returns:
- the InputMap used for key bindings.
-
bind
public void bind(javax.swing.KeyStroke keyStroke, javax.swing.Action action)Binds the keyStroke and action, a key is auto-created.- Parameters:
keyStroke- the keyStroke to bindaction- the action to bind.
-
put
public void put(javax.swing.KeyStroke keyStroke, java.lang.Object actionMapKey)Registers a binding forkeyStroketoactionMapKey. Semantics are the same as the corresponding method in InputMap.- Parameters:
keyStroke- the keyStroke to bindkey- the key used for binding.- See Also:
InputMap.put(KeyStroke, Object)
-
get
public java.lang.Object get(javax.swing.KeyStroke keyStroke)
Returns the actionMapKey registerd for the keyStroke.- Parameters:
keyStroke- the keyStroke to bind- Returns:
- the actionMapKey for the keyStroke or null if unbound.
-
put
public void put(java.lang.Object key, javax.swing.Action action)Registers a binding forkeytoaction. 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:
ActionMap.put(Object, Action)
-
get
public javax.swing.Action get(java.lang.Object key)
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.
-
-
DataMelt 3.0 © DataMelt by jWork.ORG