metis.input.keybinding
A system for handling keyboard shortcuts. Given a table of keybindings and
their corresponding action, metis.input.keybinding
will process events and
invoke the appropriate action when a key is pressed.
Keybinding syntax
Keybindings are written in an Emacs-esque notation, specifying the modifier keys and then the actual key. For instance, C-M-underscore means the "Control", "Meta" (or "Alt") and "underscore" keys must be pressed.
Usage
local kb = require "metis.input.keybinding".create { ["C-M-x"] = function() print("Pressed Ctrl, Alt and 'x'") end, char = function(c) print("Typed character " .. c) end, } while true do kb:event(os.pullEvent()) end
create_keymap(...) | Create a new keymap. |
---|---|
create([keymap]) | Create a new group of keybindings. |
- create_keymap(...)Source
Create a new keymap.
Parameters
- ... { [
string
] =function
} |Keymap
One or more tables of key bindings. These can either be:
- An existing
Keymap
instance or. - A mapping of keybinding names (as described in the module description) to functions. The special name "char" may be used when a character is typed.
Later keybindings override existing ones.
- An existing
Or
- ...
Keymap
An existing keymap which this one will extend.
Returns
Keymap
The constructed keymap handler.
- ... { [
- create([keymap])Source
Create a new group of keybindings.
Parameters
- keymap?
Keymap
| { [string
] =function
} The keymap to use for this instance. Can be changed later withKeybindings:set_keymap
.
Returns
Keybindings
The constructed keybinding handler.
- keymap?
Types
Keybindings
The keybinding processor. This accepts events, determines what keys are pressed, and dispatches the appropriate action.
- Keybindings.char(chr, ...)Source
Process a
char
event.Parameters
- chr
string
The character which has been typed. - ... Additional arguments which will be passed to the associated action.
Returns
- The result of the associated keybinding's action, or
nil
.
- chr
- Keybindings.key(key, ...)Source
Process a
key
event.Parameters
- key
number
The key which has been pressed. - ... Additional arguments which will be passed to the associated action.
Returns
- The result of the associated keybinding's action, or
nil
.
- key
- Keybindings.key_up(key)Source
Process a
key_up
event.Parameters
- key
number
The key which has been released.
- key
- Keybindings.event(event, ...)Source
Process an event. This dispatches to the
Keybindings:key
,Keybindings:key_up
orKeybindings:char
as appropriate.Repeat key events will be passed to
Keybindings:key
by default. If this is not desired, your keybindings should either check if this event is a repeat (the first argument will betrue
) or roll your own version ofevent
.Parameters
- event
string
The name of the event. - ... Additional event arguments.
Usage
kb:event(os.pullEvent())
- event
- Keybindings.set_keymap(keymap)Source
Update the underlying
Keymap
of this keybindings instance.Parameters
- Keybindings:reset()Source
Reset the set of pressed keys within the keybinding manager. This may be used if a window becomes unfocused, and so
key_up
events will no longer be sent to it.
Keymap
A mapping of various key combinations to functions.
This is used by a Keybindings
instance to dispatch
See also
create_keymap
to create a newKeymap
.