Use tinykeys for simple keybindings
mainTo register keybindings globally, import tinykeys and call it with a target element (e.g., window) and a mapping object. The mapping object keys are strings representing the keybinding syntax, and values are the callback functions to execute.
Supported syntax includes:
- Single keys (matches
event.keyorevent.code) - Modifiers (e.g.,
Shift+D) - Optional modifiers (e.g.,
[Shift]+D) - The
$modalias (Mac:Meta, Windows/Linux:Control) - Regular expressions for multiple keys (e.g.,
$mod+([0-9])) - Sequences of presses (e.g.,
y e e t)
import { tinykeys } from "tinykeys"
tinykeys(window, {
"Shift+D": () => {
alert("The 'Shift' and 'd' keys were pressed at the same time")
},
"y e e t": () => {
alert("The keys 'y', 'e', 'e', and 't' were pressed in order")
},
"$mod+([0-9])": event => {
event.preventDefault()
alert(`Either 'Control+${event.key}' or 'Meta+${event.key}' were pressed`)
},
})