Editors, mail clients and dashboards all have keyboard shortcuts: Ctrl+S to save, Cmd+K to search on a Mac (Ctrl+K elsewhere), ? to list the shortcuts, j and k to move between items. Getting them right takes more than comparing event.key: modifiers must match exactly, a single-letter shortcut must not fire while someone types in a field, held keys repeat, and some keyboards change the character a modifier produces.
Write createShortcuts(target, options) in shortcuts.js.
functioncreateShortcuts(target, options) // returns { add, destroy }constkeys=createShortcuts(document, { mac: false });constremove= keys.add("ctrl+shift+p", (event) =>openPalette(), { repeat: false });remove(); // removes that shortcutkeys.destroy(); // removes every shortcut and the listener
options.mac: whether mod means meta (true) or ctrl (false). When it is missing, guess from navigator.platform.
A combo is zero or more modifiers (ctrl, alt, shift, meta, mod) and one key, joined by +, in any case and any order. The key is an event.key value (s, escape, arrowdown, ?), or space for " " and plus for "+".
createShortcuts adds onekeydown listener to target, which hears keys pressed anywhere inside it. Other events (such as keyup) are ignored.
A press matches a combo when ctrlKey, altKey, shiftKey and metaKey are exactly the combo's modifiers, and the key matches.
Keys are compared without case, so shift+p matches event.key"P". Symbols need the modifiers that produce them: ? is shift+?.
When event.key does not match a letter or digit, event.code is checked: KeyS for s, Digit1 for 1. (On a Mac, Option+S types ß, and Shift+1 types !.)
mod is meta when mac is true, ctrl otherwise.
A match calls event.preventDefault() and then the handler. Keys that match nothing are left alone.
While the event's target is an input, textarea, select or editable content (isContentEditable), combos without ctrl, alt or meta are ignored (shift alone does not count).
Held keys (event.repeat) are ignored unless the shortcut was added with { repeat: true }. Keys pressed during text composition (event.isComposing) are ignored.
Every handler whose combo matches runs, in the order added. A shortcut added by a handler runs from the next press, not during the current one. A shortcut removed by a handler stops at once: if it has not run yet in the current press, it does not run.
add returns a function that removes that one shortcut (calling it twice is harmless). destroy removes the listener and all shortcuts. Two sets of shortcuts are independent.
An invalid combo throws a SyntaxError: an empty combo, no key (ctrl, ctrl+shift, shift+mod), an unknown modifier (cmd+k, a+b), or a modifier used twice.
constkeys=createShortcuts(document, { mac: true });keys.add("mod+k", openSearch); // Cmd+Kkeys.add("shift+?", showHelp); // Shift+/ on a US keyboardkeys.add("j", nextItem); // not while typing in a fieldkeys.add("arrowdown", moveDown, { repeat: true });
JAVASCRIPT
// Ctrl+Shift+S does not run "ctrl+s"; Ctrl+S does, and the browser's Save dialog stays closed.keys.add("ctrl+s", save);
JAVASCRIPT
keys.add("cmd+k", openSearch); // throws SyntaxError: use meta or mod
Plain JavaScript in a browser page; the tests call createShortcuts on elements they build and dispatch real KeyboardEvents with key, code and modifier flags.
Sequences (g then i), different layouts' symbol positions and remapping by users are out of scope.