public class MapPaneKeyHandler extends KeyAdapter
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 );
KeyInfo,
AbstractMapPane#setKeyHandler(java.awt.event.KeyListener)| Modifier and Type | Class and Description |
|---|---|
static class |
MapPaneKeyHandler.Action
Constants for supported actions.
|
| Constructor and Description |
|---|
MapPaneKeyHandler(MapPane mapPane)
Creates a new instance with the default key bindings for actions.
|
| Modifier and Type | Method and Description |
|---|---|
KeyInfo |
getBindingForAction(MapPaneKeyHandler.Action action)
Gets the current key binding for the given action.
|
Map<KeyInfo,MapPaneKeyHandler.Action> |
getBindings()
Gets the current key bindings.
|
void |
keyPressed(KeyEvent e)
Handles a key-pressed event.
|
void |
setAllBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
Sets the bindings to those specified in
newBindings. |
void |
setBinding(KeyInfo keyInfo,
MapPaneKeyHandler.Action action)
Sets the key binding for a single action.
|
void |
setBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
Sets one or more key bindings for actions.
|
void |
setDefaultBindings()
Sets all key bindings to their default value.
|
keyReleased, keyTypedpublic MapPaneKeyHandler(MapPane mapPane)
mapPane - the map pane associated with this handlerpublic void setDefaultBindings()
public Map<KeyInfo,MapPaneKeyHandler.Action> getBindings()
Map, so
subsequent changes to it will not affect this handler.public KeyInfo getBindingForAction(MapPaneKeyHandler.Action action)
action - the actionnull if there is no bindingIllegalArgumentException - if action is nullpublic void setBinding(KeyInfo keyInfo, MapPaneKeyHandler.Action action)
keyInfo - the key bindingaction - the actionIllegalArgumentException - if either argument is nullpublic void setBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
newBindings - new key bindingsIllegalArgumentException - if newBindings is nullpublic void setAllBindings(Map<KeyInfo,MapPaneKeyHandler.Action> newBindings)
newBindings. This method differs to
setBindings(java.util.Map) in that any actions which do not appear in the input
map are disabled.newBindings - new key bindingsIllegalArgumentException - if newBindings is nullpublic void keyPressed(KeyEvent e)
keyPressed in interface KeyListenerkeyPressed in class KeyAdaptere - input key eventCopyright © 1996–2019 Geotools. All rights reserved.