Class 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 );
    Michael Bedward
    See Also:
    KeyInfo, AbstractMapPane.setKeyHandler(KeyListener)
    • Constructor Detail

      • MapPaneKeyHandler

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

      • 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.
        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.
        action - the action
        the key binding; or null if there is no binding
        IllegalArgumentException - if action 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.
        newBindings - new key bindings
        IllegalArgumentException - if newBindings is null