Class MapPaneKeyHandler

Object
KeyAdapter
MapPaneKeyHandler
All Implemented Interfaces:
KeyListener, EventListener

public class MapPaneKeyHandler extends KeyAdapter
Handles keyboard events for a map pane. This is the default handler for classes derived from AbstractMapPane. It provides for keyboard-controlled scrolling and zooming of the display. The default key bindings for actions should be suitable for most keyboards.

While the Java Swing toolkit provides its own mechanism for linking key events to actions, this class is somewhat easier to use and provides a model that could be implemented in other toolkits such as SWT. However, you are free to ignore this class and use your own key handler instead since the map pane classes only require that the handler implements the KeyListener interface.

Key bindings for an individual action can be set like this:


 // Bind left-scroll action to the 'h' key (for Vim fans)
 KeyInfo key = new KeyInfo(KeyEvent.VK_H, 0);
 mapPaneKeyHandler.setBinding(key, MapPaneKeyHandler.Action.SCROLL_LEFT);
 
Multiple bindings can be set with the setBindings(Map) or setAllBindings(Map) methods:

 Map<KeyInfo, MapPaneKeyHandler.Action> bindings =
         new HashMap<KeyInfo, MapPaneKeyHandler.Action>();

 bindings.put(new KeyInfo(KeyEvent.VK_H, 0), MapPaneKeyHandler.Action.SCROLL_LEFT);
 bindings.put(new KeyInfo(KeyEvent.VK_L, 0), MapPaneKeyHandler.Action.SCROLL_RIGHT);
 bindings.put(new KeyInfo(KeyEvent.VK_K, 0), MapPaneKeyHandler.Action.SCROLL_UP);
 bindings.put(new KeyInfo(KeyEvent.VK_J, 0), MapPaneKeyHandler.Action.SCROLL_DOWN);

 mapPaneKeyHandler.setBindings( bindings );
 
Since:
8.0
Author:
Michael Bedward
See Also:
  • Constructor Details

    • MapPaneKeyHandler

      public MapPaneKeyHandler(MapPane mapPane)
      Creates a new instance with the default key bindings for actions.
      Parameters:
      mapPane - the map pane associated with this handler
  • Method Details

    • setDefaultBindings

      public void setDefaultBindings()
      Sets all key bindings to their default value.
    • getBindings

      public Map<KeyInfo,MapPaneKeyHandler.Action> getBindings()
      Gets the current key bindings. The bindings are copied into the destination Map, so subsequent changes to it will not affect this handler.
      Returns:
      the current key bindings
    • getBindingForAction

      public KeyInfo getBindingForAction(MapPaneKeyHandler.Action action)
      Gets the current key binding for the given action. The object returned is a copy.
      Parameters:
      action - the action
      Returns:
      the key binding; or null if there is no binding
      Throws:
      IllegalArgumentException - if action is null
    • setBinding

      public void setBinding(KeyInfo keyInfo, MapPaneKeyHandler.Action action)
      Sets the key binding for a single action.
      Parameters:
      keyInfo - the key binding
      action - the action
      Throws:
      IllegalArgumentException - if either argument is null
    • setBindings

      public void setBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
      Sets one or more key bindings for actions. This method can be used to set a subset of the key bindings while leaving others unchanged.
      Parameters:
      newBindings - new key bindings
      Throws:
      IllegalArgumentException - if newBindings is null
    • setAllBindings

      public void setAllBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
      Sets the bindings to those specified in newBindings. This method differs to setBindings(java.util.Map) in that any actions which do not appear in the input map are disabled.
      Parameters:
      newBindings - new key bindings
      Throws:
      IllegalArgumentException - if newBindings is null
    • keyPressed

      public void keyPressed(KeyEvent e)
      Handles a key-pressed event.
      Specified by:
      keyPressed in interface KeyListener
      Overrides:
      keyPressed in class KeyAdapter
      Parameters:
      e - input key event