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} |KeymapOne or more tables of key bindings. These can either be:
- An existing
Keymapinstance 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
- ...
KeymapAn existing keymap which this one will extend.
Returns
KeymapThe 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
KeybindingsThe 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
charevent.Parameters
- chr
stringThe 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
keyevent.Parameters
- key
numberThe 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_upevent.Parameters
- key
numberThe key which has been released.
- key
- Keybindings.event(event, ...)Source
Process an event. This dispatches to the
Keybindings:key,Keybindings:key_uporKeybindings:charas appropriate.Repeat key events will be passed to
Keybindings:keyby 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
stringThe name of the event. - ... Additional event arguments.
Usage
kb:event(os.pullEvent())
- event
- Keybindings.set_keymap(keymap)Source
Update the underlying
Keymapof 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_upevents 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_keymapto create a newKeymap.