| 1 |
# `tinykeys` |
| 2 |
|
| 3 |
> A tiny (~1KB) & modern library for keybindings. |
| 4 |
> [See Demo](https://jamiebuilds.github.io/tinykeys/) |
| 5 |
|
| 6 |
## Install |
| 7 |
|
| 8 |
```sh |
| 9 |
npm install --save tinykeys |
| 10 |
``` |
| 11 |
|
| 12 |
Or for a CDN version, you can use it on [unpkg.com](https://unpkg.com/tinykeys) |
| 13 |
|
| 14 |
## Usage |
| 15 |
|
| 16 |
```js |
| 17 |
import { tinykeys } from "tinykeys" // Or `window.tinykeys` using the CDN version |
| 18 |
|
| 19 |
tinykeys(window, { |
| 20 |
"Shift+D": () => { |
| 21 |
alert("The 'Shift' and 'd' keys were pressed at the same time") |
| 22 |
}, |
| 23 |
"y e e t": () => { |
| 24 |
alert("The keys 'y', 'e', 'e', and 't' were pressed in order") |
| 25 |
}, |
| 26 |
"$mod+([0-9])": event => { |
| 27 |
event.preventDefault() |
| 28 |
alert(`Either 'Control+${event.key}' or 'Meta+${event.key}' were pressed`) |
| 29 |
}, |
| 30 |
}) |
| 31 |
``` |
| 32 |
|
| 33 |
Alternatively, if you want to only create the keybinding handler, and register |
| 34 |
it as an event listener yourself: |
| 35 |
|
| 36 |
```js |
| 37 |
import { createKeybindingsHandler } from "tinykeys" |
| 38 |
|
| 39 |
let handler = createKeybindingsHandler({ |
| 40 |
"Shift+D": () => { |
| 41 |
alert("The 'Shift' and 'd' keys were pressed at the same time") |
| 42 |
}, |
| 43 |
"y e e t": () => { |
| 44 |
alert("The keys 'y', 'e', 'e', and 't' were pressed in order") |
| 45 |
}, |
| 46 |
"$mod+KeyD": event => { |
| 47 |
event.preventDefault() |
| 48 |
alert("Either 'Control+d' or 'Meta+d' were pressed") |
| 49 |
}, |
| 50 |
}) |
| 51 |
|
| 52 |
window.addEventListener("keydown", handler) |
| 53 |
``` |
| 54 |
|
| 55 |
### React Hooks Example |
| 56 |
|
| 57 |
If you're using tinykeys within a component, you should also make use of the |
| 58 |
returned `unsubscribe()` function. |
| 59 |
|
| 60 |
```js |
| 61 |
import { useEffect } from "react" |
| 62 |
import { tinykeys } from "tinykeys" |
| 63 |
|
| 64 |
function useKeyboardShortcuts() { |
| 65 |
useEffect(() => { |
| 66 |
let unsubscribe = tinykeys(window, { |
| 67 |
// ... |
| 68 |
}) |
| 69 |
return () => { |
| 70 |
unsubscribe() |
| 71 |
} |
| 72 |
}) |
| 73 |
} |
| 74 |
``` |
| 75 |
|
| 76 |
## Commonly used `key`'s and `code`'s |
| 77 |
|
| 78 |
Keybindings will be matched against |
| 79 |
[`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) |
| 80 |
and[`KeyboardEvent.code`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values) |
| 81 |
which may have some names you don't expect. |
| 82 |
|
| 83 |
| Windows | macOS | `key` | `code` | |
| 84 |
| ------------- | --------------- | ------------- | ------------------------------ | |
| 85 |
| N/A | `Command` / `⌘` | `Meta` | `MetaLeft` / `MetaRight` | |
| 86 |
| `Alt` | `Option` / `⌥` | `Alt` | `AltLeft` / `AltRight` | |
| 87 |
| `Control` | `Control` / `^` | `Control` | `ControlLeft` / `ControlRight` | |
| 88 |
| `Shift` | `Shift` | `Shift` | `ShiftLeft` / `ShiftRight` | |
| 89 |
| `Space` | `Space` | N/A | `Space` | |
| 90 |
| `Enter` | `Return` | `Enter` | `Enter` | |
| 91 |
| `Esc` | `Esc` | `Escape` | `Escape` | |
| 92 |
| `1`, `2`, etc | `1`, `2`, etc | `1`, `2`, etc | `Digit1`, `Digit2`, etc | |
| 93 |
| `a`, `b`, etc | `a`, `b`, etc | `a`, `b`, etc | `KeyA`, `KeyB`, etc | |
| 94 |
| `-` | `-` | `-` | `Minus` | |
| 95 |
| `=` | `=` | `=` | `Equal` | |
| 96 |
| `+` | `+` | `+` | `Equal`\* | |
| 97 |
|
| 98 |
Something missing? Check out the key logger on the |
| 99 |
[demo website](https://jamiebuilds.github.io/tinykeys/) |
| 100 |
|
| 101 |
> _\* Some keys will have the same code as others because they appear on the |
| 102 |
> same key on the keyboard. Keep in mind how this is affected by international |
| 103 |
> keyboards which may have different layouts._ |
| 104 |
|
| 105 |
## Key aliases |
| 106 |
|
| 107 |
In some instances, tinykeys will alias keys depending on the platform to |
| 108 |
simplify cross-platform keybindings on international keyboards. |
| 109 |
|
| 110 |
### `AltGraph` (modifier) |
| 111 |
|
| 112 |
On Windows, on many non-US standard keyboard layouts, there is a key named |
| 113 |
`Alt Gr` or `AltGraph` in the browser, in some browsers, pressing `Control+Alt` |
| 114 |
will report `AltGraph` as being pressed instead. |
| 115 |
|
| 116 |
Similarly on macOS, the `Alt` (`Option`) key will sometimes be reported as the |
| 117 |
`AltGraph` key. |
| 118 |
|
| 119 |
**Note:** The purpose of the `Alt Gr` key is to type "Alternate Graphics" so you |
| 120 |
will often want to use the `event.code` (`KeyS`) for letters instead of |
| 121 |
`event.key` (`S`) |
| 122 |
|
| 123 |
```js |
| 124 |
tinykeys(window, { |
| 125 |
"Control+Alt+KeyS": event => { |
| 126 |
// macOS: `Control+Alt+S` or `Control+AltGraph+S` |
| 127 |
// Windows: `Control+Alt+S` or `Control+AltGraph+S` or `AltGraph+S` |
| 128 |
}, |
| 129 |
"$mod+Alt+KeyS": event => { |
| 130 |
// macOS: `Meta+Alt+S` or `Meta+AltGraph+S` |
| 131 |
// Windows: `Control+Alt+S` or `Control+AltGraph+S` or `AltGraph+S` |
| 132 |
}, |
| 133 |
}) |
| 134 |
``` |
| 135 |
|
| 136 |
## Keybinding Syntax |
| 137 |
|
| 138 |
Keybindings are made up of a **_sequence_** of **_presses_**. |
| 139 |
|
| 140 |
A **_press_** can be as simple as a single **_key_** which matches against |
| 141 |
[`KeyboardEvent.code`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/code/code_values) |
| 142 |
and |
| 143 |
[`KeyboardEvent.key`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key) |
| 144 |
(case-insensitive). |
| 145 |
|
| 146 |
```js |
| 147 |
// Matches `event.key`: |
| 148 |
"d" |
| 149 |
// Matches: `event.code`: |
| 150 |
"KeyD" |
| 151 |
``` |
| 152 |
|
| 153 |
Presses can optionally be prefixed with **_modifiers_** which match against any |
| 154 |
valid value to |
| 155 |
[`KeyboardEvent.getModifierState()`](https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/getModifierState). |
| 156 |
|
| 157 |
```js |
| 158 |
"Control+d" |
| 159 |
"Meta+d" |
| 160 |
"Shift+D" |
| 161 |
"Alt+KeyD" |
| 162 |
"Meta+Shift+D" |
| 163 |
``` |
| 164 |
|
| 165 |
Modifiers can be made optional by wrapping them with `[brackets]` |
| 166 |
|
| 167 |
```js |
| 168 |
"[Shift]+?" |
| 169 |
"Control+[Shift]+D" |
| 170 |
``` |
| 171 |
|
| 172 |
There is also a special `$mod` modifier that makes it easy to support cross |
| 173 |
platform keybindings: |
| 174 |
|
| 175 |
- Mac: `$mod` = `Meta` (⌘) |
| 176 |
- Windows/Linux: `$mod` = `Control` |
| 177 |
|
| 178 |
```js |
| 179 |
"$mod+D" // Meta/Control+D |
| 180 |
"$mod+Shift+D" // Meta/Control+Shift+D |
| 181 |
``` |
| 182 |
|
| 183 |
You can also use parenthesis to use case-insensitive regular expressions to |
| 184 |
match multiple keys (Note: This does not work for modifiers). |
| 185 |
|
| 186 |
```js |
| 187 |
"$mod+([0-9])" // $mod+0, $mod+1, $mod+2, etc... |
| 188 |
// equivalent regex: /^(?:[0-9])$/iv |
| 189 |
``` |
| 190 |
|
| 191 |
### Keybinding Sequences |
| 192 |
|
| 193 |
Keybindings can also consist of several key presses in a row: |
| 194 |
|
| 195 |
```js |
| 196 |
"g i" // i.e. "Go to Inbox" |
| 197 |
"g a" // i.e. "Go to Archive" |
| 198 |
"ArrowUp ArrowUp ArrowDown ArrowDown ArrowLeft ArrowRight ArrowLeft ArrowRight B A" |
| 199 |
``` |
| 200 |
|
| 201 |
Each press can optionally be prefixed with modifier keys: |
| 202 |
|
| 203 |
```js |
| 204 |
"$mod+K $mod+1" // i.e. "Toggle Level 1" |
| 205 |
"$mod+K $mod+2" // i.e. "Toggle Level 2" |
| 206 |
"$mod+K $mod+3" // i.e. "Toggle Level 3" |
| 207 |
``` |
| 208 |
|
| 209 |
Each press in the sequence must be pressed within 1000ms of the last. |
| 210 |
|
| 211 |
#### Keybinding Priorities |
| 212 |
|
| 213 |
It should rarely come up, but the order of your keybindings matters. So if you |
| 214 |
were to create two keybindings that would match the same event, only the first |
| 215 |
will fire. |
| 216 |
|
| 217 |
This mainly comes up when you create bindings for both `KeyboardEvent#key` and |
| 218 |
`KeyboardEvent#code` in order to support more keyboard layouts: |
| 219 |
|
| 220 |
```js |
| 221 |
tinykeys(window, { |
| 222 |
"$mod+b": () => console.log("make bold"), |
| 223 |
"$mod+KeyB": () => console.log("also bold"), |
| 224 |
}) |
| 225 |
``` |
| 226 |
|
| 227 |
> In this case, if the user holds `$mod` and types `b (KeyB)` it will only |
| 228 |
> trigger `$mod+B` and log `"make bold"` |
| 229 |
|
| 230 |
This can also come up when you have sequences that overlap with other |
| 231 |
keybindings. |
| 232 |
|
| 233 |
<!-- prettier-ignore --> |
| 234 |
```js |
| 235 |
tinykeys(window, { |
| 236 |
"g a": () => console.log("goto archive"), |
| 237 |
"a": () => console.log("archive item"), |
| 238 |
}) |
| 239 |
``` |
| 240 |
|
| 241 |
> In this case, if the user types `g a` it will only log `"goto archive"` |
| 242 |
|
| 243 |
However, you can also break later keybindings by declaring earlier keybindings |
| 244 |
that will always win: |
| 245 |
|
| 246 |
<!-- prettier-ignore --> |
| 247 |
```js |
| 248 |
tinykeys(window, { |
| 249 |
"g": () => console.log("show goto indicator"), |
| 250 |
"g a": () => console.log("goto archive"), |
| 251 |
}) |
| 252 |
``` |
| 253 |
|
| 254 |
> In this case, if the user types `g a` it will only log `"show goto indicator"` |
| 255 |
> (after the first `g`) |
| 256 |
|
| 257 |
#### Keybinding Sequence Conflicts |
| 258 |
|
| 259 |
In some circumstances, overlapping keybindings can cause "conflicts" where a |
| 260 |
keybinding has been completely typed, but there is another keybinding declared |
| 261 |
earlier that might still match. |
| 262 |
|
| 263 |
```js |
| 264 |
tinykeys(window, { |
| 265 |
"a b c": () => console.log("abc"), |
| 266 |
"a b": () => console.log("ab"), |
| 267 |
}) |
| 268 |
``` |
| 269 |
|
| 270 |
> In this case, if the user types `a b` it will not trigger either keybinding, |
| 271 |
> but it will print this warning to the console: |
| 272 |
> |
| 273 |
> ``` |
| 274 |
> warning: tinykeys: Conflict found, "a b" did not fire, waiting for: ["a b c"] |
| 275 |
> ``` |
| 276 |
|
| 277 |
### Display the keyboard sequence |
| 278 |
|
| 279 |
You can use the `parseKeybinding` method to get a structured representation of a |
| 280 |
keyboard shortcut. It can be useful when you want to display it in a fancier way |
| 281 |
than a plain string. |
| 282 |
|
| 283 |
```js |
| 284 |
import { parseKeybinding } from "tinykeys" |
| 285 |
|
| 286 |
let parsedShortcut = parseKeybinding("$mod+Shift+K $mod+1") |
| 287 |
``` |
| 288 |
|
| 289 |
Results into: |
| 290 |
|
| 291 |
<!-- prettier-ignore --> |
| 292 |
```js |
| 293 |
[ |
| 294 |
[["Meta", "Shift"], "K"], |
| 295 |
[["Meta"], "1"], |
| 296 |
] |
| 297 |
``` |
| 298 |
|
| 299 |
## Additional Configuration Options |
| 300 |
|
| 301 |
You can configure the behavior of tinykeys in a couple ways using a third |
| 302 |
`options` parameter. |
| 303 |
|
| 304 |
```js |
| 305 |
tinykeys( |
| 306 |
window, |
| 307 |
{ |
| 308 |
M: toggleMute, |
| 309 |
}, |
| 310 |
{ |
| 311 |
event: "keyup", |
| 312 |
capture: true, |
| 313 |
}, |
| 314 |
) |
| 315 |
``` |
| 316 |
|
| 317 |
### `options.event` |
| 318 |
|
| 319 |
Valid values: `"keydown"`, `"keyup"` |
| 320 |
|
| 321 |
Key presses will listen to this event (default: `"keydown"`). |
| 322 |
|
| 323 |
> **Note:** Do not pass `"keypress"`, it is deprecated in browsers. |
| 324 |
|
| 325 |
### `options.timeout` |
| 326 |
|
| 327 |
Keybinding sequences will wait this long between key presses before cancelling |
| 328 |
(default: `1000`). |
| 329 |
|
| 330 |
> **Note:** Setting this value too low (i.e. `300`) will be too fast for many of |
| 331 |
> your users. |
| 332 |
|
| 333 |
### `options.ignore` |
| 334 |
|
| 335 |
Add a filter for which keyboard events should be ignored. |
| 336 |
|
| 337 |
By default, tinykeys will ignore keyboard events from `[contenteditable]`, |
| 338 |
`input`, `textarea`, and `select` unless they are the `event.currentTarget`. |