Key Mapping

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

defineKey(JComponent jComponent , String keystroke , PnutsFunction func { , int condition } ) or
(JComponent jComponent , String keystroke , Action action { , int condition } ) or
(JTextComponent jTextComponent , Object[] array_of_keystroke , PnutsFunction func ) or
(JTextComponent jTextComponent , Object[] array_of_keystroke , Action action )

defineKey() registers a keystroke to invoke the func or the action. This function is assumed to be used with Swing.

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.

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 keymap , String keystroke , PnutsFunction func ) or
(Keymap keymap , String keystroke , Action action )

modifyKey() modifies a swing Keymap object.

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

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


Back