A minimal, zero-dependency utility for handling keyboard shortcuts. Provides a clean API for registering and managing key bindings in TypeScript and JavaScript applications.
This library registers and listens to keyboard input, executing actions when the keystrokes match a user-defined shortcut.
npm install --save @hyperse/tinykeys
key's and code'sKeybindings will be matched against
KeyboardEvent.key
andKeyboardEvent.code
which may have some names you don't expect.
| Windows | macOS | key | code |
|---|---|---|---|
| N/A | Command / ⌘ | Meta | MetaLeft / MetaRight |
Alt | Option / ⌥ | Alt | AltLeft / AltRight |
Control | Control / |
In some instances, tinykeys will alias keys depending on the platform to simplify cross-platform keybindings on international keyboards.
AltGraph (modifier)On Windows, on many non-US standard keyboard layouts, there is a key named
Alt Gr or AltGraph in the browser, in some browsers, pressing Control+Alt
will report AltGraph as being pressed instead.
Similarly on macOS, the Alt (Option) key will sometimes be reported as the
AltGraph key.
Note: The purpose of the Alt Gr key is to type "Alternate Graphics" so you
will often want to use the event.code (KeyS) for letters instead of
event.key (S)
Keybindings are made up of a sequence of presses.
A press can be as simple as a single key which matches against
KeyboardEvent.code
and
KeyboardEvent.key
(case-insensitive).
// Matches `event.key`: 'd'; // Matches: `event.code`: 'KeyD';
Presses can optionally be prefixed with modifiers which match against any
valid value to
KeyboardEvent.getModifierState().
'Control+d'; 'Meta+d'; 'Shift+D';
There is also a special $mod modifier that makes it easy to support cross
platform keybindings:
$mod = Meta (⌘)$mod = Control'$mod+D'; // Meta/Control+D '$mod+Shift+D'; // Meta/Control+Shift+D
Alternatively, you can use parenthesis to use case-sensitive regular expressions to match multiple keys.
'$mod+([0-9])'; // $mod+0, $mod+1, $mod+2, etc... // equivalent regex: /^[0-9]$/
Keybindings can also consist of several key presses in a row:
'g i'; // i.e. "Go to Inbox" 'g a'; // i.e. "Go to Archive" 'ArrowUp ArrowUp ArrowDown ArrowDown ArrowLeft ArrowRight ArrowLeft ArrowRight B A';
Each press can optionally be prefixed with modifier keys:
'$mod+K $mod+1'; // i.e. "Toggle Level 1" '$mod+K $mod+2'; // i.e. "Toggle Level 2" '$mod+K $mod+3'; // i.e. "Toggle Level 3"
Each press in the sequence must be pressed within 1000ms of the last.
You can configure the behavior of tinykeys in a couple ways using a third
options parameter.
options.eventValid values: "keydown", "keyup"
Key presses will listen to this event (default: "keydown").
Note: Do not pass
"keypress", it is deprecated in browsers.
options.timeoutKeybinding sequences will wait this long between key presses before cancelling
(default: 1000).
Note: Setting this value too low (i.e.
300) will be too fast for many of your users.
^Control |
ControlLeft / ControlRight |
Shift | Shift | Shift | ShiftLeft / ShiftRight |
Space | Space | N/A | Space |
Enter | Return | Enter | Enter |
Esc | Esc | Escape | Escape |
1, 2, etc | 1, 2, etc | 1, 2, etc | Digit1, Digit2, etc |
a, b, etc | a, b, etc | a, b, etc | KeyA, KeyB, etc |
- | - | - | Minus |
= | = | = | Equal |
+ | + | + | Equal* |