Class KeyBindingDispatcher

java.lang.Object
org.jdesktop.swingx.event.KeyBindingDispatcher
All Implemented Interfaces:
KeyEventDispatcher

public class KeyBindingDispatcher extends Object implements 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 Details

    • KeyBindingDispatcher

      public KeyBindingDispatcher()
  • Method Details

    • dispatchKeyEvent

      public boolean dispatchKeyEvent(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 well

      Subclasses may re-implement to return true and end the dispatch here.

      Specified by:
      dispatchKeyEvent in interface KeyEventDispatcher
      Parameters:
      e - the keyEvent to dispatch
      Returns:
      unconditionally false
    • processKeyBinding

      protected boolean processKeyBinding(KeyEvent e)
      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

      protected Action getAction(KeyEvent e)
      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

      protected ActionEvent createActionEvent(KeyEvent e, Action action)
      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 action
      action - the bound action
      Returns:
      the actionEvent that will be passed into the action's actionPerformed.
      See Also:
    • bind

      public void bind(KeyStroke keyStroke, Action action, Object key)
      Binds the keyStroke and action, using the given key which must not be null.

      Parameters:
      keyStroke - the keyStroke to bind
      action - the action to bind.
      key - the key used for binding, must not be null.
    • getActionMap

      public ActionMap getActionMap()
      Returns the ActionMap used for key bindings, lazily created if not yet done.
      Returns:
      the ActionMap used for key bindings
    • getInputMap

      public 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(KeyStroke keyStroke, Action action)
      Binds the keyStroke and action, a key is auto-created.
      Parameters:
      keyStroke - the keyStroke to bind
      action - the action to bind.
    • createAutoKey

      protected Object createAutoKey()
      Creates and returns a unique actionMapKey.
      Returns:
      a unique key used for binding in input/actionMap.
    • put

      public void put(KeyStroke keyStroke, Object actionMapKey)
      Registers a binding for keyStroke to actionMapKey. Semantics are the same as the corresponding method in InputMap.
      Parameters:
      keyStroke - the keyStroke to bind
      actionMapKey - the key used for binding.
      See Also:
    • get

      public Object get(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(Object key, Action action)
      Registers a binding for key to action. 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

      public Action get(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.