Key Mapping

A key sequence can be bound to a function. See a sample script to know a typical usage.

defineKey( jComponent , keystroke , funcOrAction [ , condition ] )
defineKey( jTextComponent , array_of_keystroke , funcOrAction )

defineKey() registers a keystroke to invoke the funcOrAction. This function is assumed to be used with swing.

jComponent is a instance of JComponent.

keystroke is a String in the following syntax. keystroke is case-insensitive.

    keystroke ::= [ modifiers - ] key [ ! ]
    modifiers ::= modifierKey [ + modifierKey ]*
    modifierKey ::= meta | ctrl | alt | shift
    key ::= {# | VK_# is defined in java.awt.event.KeyEvent}
When keystroke ends with '!' it represents key-released event, otherwise key-pressed event.

funcOrAction is a Pnuts function or a Action in swing.

condition is one of:

e.g.
defineKey(JTextPane(), "Ctrl+alt-K", function (e) println(e))
defineKey(JTextPane(), "X!", function (e) println(e))

When the first parameter is an instance of JTextComponent, key mappings of multiple keystrokes can be defined.

e.g.
defineKey(JTextPane(), ["Ctrl-X", "Ctrl-F"], function (e) println(e))
modifyKey( keymap , keystroke , funcOrAction )

modifyKey() modifies a swing Keymap object.

e.g.
pane = JTextField()
map = pane.getKeymap()
modifyKey(map, "ctrl-x", function (e) println(e))
getKeyStroke( keystroke )

getKeyStroke() makes a swing KeyStroke object from a keystroke string.


Back