hotkey-router 0.3.1 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +129 -11
- package/dist/auto.js +5 -1031
- package/dist/auto.js.map +4 -4
- package/dist/auto.min.js +2 -2
- package/dist/format.js +394 -0
- package/dist/format.js.map +7 -0
- package/dist/hotkey-router.cjs +270 -127
- package/dist/hotkey-router.cjs.map +4 -4
- package/dist/hotkey-router.js +269 -128
- package/dist/hotkey-router.js.map +4 -4
- package/dist/hotkey-router.min.js +2 -2
- package/dist/reservations.js +128 -62
- package/dist/reservations.js.map +4 -4
- package/dist/sites.js +195 -80
- package/dist/sites.js.map +4 -4
- package/dist/types/format.d.ts +128 -0
- package/dist/types/hotkey-router.d.ts +19 -20
- package/dist/types/parse.d.ts +125 -0
- package/dist/types/platform.d.ts +23 -0
- package/dist/types/reservations.d.ts +11 -6
- package/dist/types/sites.d.ts +6 -1
- package/dist/types/validate.d.ts +2 -2
- package/dist/validate.js +202 -8
- package/dist/validate.js.map +3 -3
- package/package.json +12 -2
package/README.md
CHANGED
|
@@ -17,7 +17,8 @@ A tiny, deterministic keyboard routing engine for modern web apps. Not a key uti
|
|
|
17
17
|
- Input-safe by default
|
|
18
18
|
- Testable via `trigger()`
|
|
19
19
|
- Optional opt-in browser/OS conflict warnings (tree-shakable)
|
|
20
|
-
-
|
|
20
|
+
- Display helpers: `Ctrl+Shift+.` / `⇧⌘.` and accessible keycaps (`hotkey-router/format`)
|
|
21
|
+
- ~4 kB minified + gzipped (core); ~8.5 kB with conflict warnings
|
|
21
22
|
- Zero dependencies
|
|
22
23
|
|
|
23
24
|
---
|
|
@@ -125,6 +126,28 @@ a character (Windows reports AltGr as Ctrl+Alt, so `ctrl+alt+code:Comma`
|
|
|
125
126
|
would otherwise swallow the `<` a Polish or Czech user typed) and while an
|
|
126
127
|
IME is composing.
|
|
127
128
|
|
|
129
|
+
`when` and `allowIn` receive the `KeyboardEvent`, not an element. To check
|
|
130
|
+
where the key was typed, read `event.composedPath()[0]` (the element inside an
|
|
131
|
+
open shadow root) or `event.target`.
|
|
132
|
+
|
|
133
|
+
**Shifted punctuation.** With Shift held, `event.key` is the shifted
|
|
134
|
+
character, so a key-based `ctrl+shift+,` waits for a `,` that a US keyboard
|
|
135
|
+
reports as `<`, and never fires. Bind the physical key instead:
|
|
136
|
+
`ctrl+shift+code:Comma`. Since v0.3.3, `bind()` warns once in the console for
|
|
137
|
+
Shift with `` , . / ; ' [ ] \ - = ` ``, and `validateCombo` rejects it with the
|
|
138
|
+
reason `shifted-punctuation`.
|
|
139
|
+
|
|
140
|
+
**Non-Latin layouts (v0.3.3+).** On a Russian layout Ctrl+K reports
|
|
141
|
+
`event.key === 'л'` with `event.code === 'KeyK'`. Like the browser's own
|
|
142
|
+
shortcuts, a key-based `ctrl+k` binding still fires there: when Ctrl or Cmd is
|
|
143
|
+
held (Shift allowed, Alt not), the key types a letter from a non-Latin script
|
|
144
|
+
(Cyrillic, Greek, Hebrew, Arabic, ...), and `code` is `KeyA`..`KeyZ`, the
|
|
145
|
+
router also looks up the Latin letter for that key. It does this only when no
|
|
146
|
+
exact binding is eligible, so a `ctrl+л` or `ctrl+code:KeyK` binding always
|
|
147
|
+
wins over it. Plain and Shift-only letters (typing), Alt and AltGr chords, IME
|
|
148
|
+
composition, digits and Latin layouts such as AZERTY are never affected. To
|
|
149
|
+
require the exact character, gate the binding: `when: (e) => e.key === 'k'`.
|
|
150
|
+
|
|
128
151
|
**Examples**
|
|
129
152
|
|
|
130
153
|
Ignore repeat keydown (default behavior):
|
|
@@ -140,6 +163,11 @@ hotkeys.bind('mod+k', openPalette, null, {
|
|
|
140
163
|
allowIn: () => true,
|
|
141
164
|
preventDefault: true,
|
|
142
165
|
})
|
|
166
|
+
|
|
167
|
+
// Only in one field (the event, not the element, is passed in)
|
|
168
|
+
hotkeys.bind('enter', submitFollowUp, null, {
|
|
169
|
+
allowIn: (e) => e.composedPath()[0] === followUpInput,
|
|
170
|
+
})
|
|
143
171
|
```
|
|
144
172
|
|
|
145
173
|
Conditional binding:
|
|
@@ -180,6 +208,8 @@ unregister()
|
|
|
180
208
|
```
|
|
181
209
|
|
|
182
210
|
Plugin cleanup is isolated. Removing one plugin never affects other bindings.
|
|
211
|
+
If any key string fails to parse, `registerPlugin` throws and binds nothing
|
|
212
|
+
(v0.3.3+).
|
|
183
213
|
|
|
184
214
|
### `unregisterPlugin(name)`
|
|
185
215
|
|
|
@@ -212,7 +242,24 @@ By default, hotkeys do **not** fire inside:
|
|
|
212
242
|
- `[contenteditable]`
|
|
213
243
|
- `role="textbox"`
|
|
214
244
|
|
|
215
|
-
|
|
245
|
+
This includes those elements inside an open shadow root (v0.3.3+; the router
|
|
246
|
+
reads `event.composedPath()[0]`). A closed shadow root hides its inside from
|
|
247
|
+
the page, so only its host element is checked there.
|
|
248
|
+
|
|
249
|
+
Every `<input>` counts, whatever its `type` and whatever modifiers are held:
|
|
250
|
+
a checkbox, radio, range slider, color or file input blocks `ctrl+k` as well
|
|
251
|
+
as Space. A native `<button>` is **not** on the list, so a plain `space` or
|
|
252
|
+
`enter` binding fires while a button has focus; gate such bindings with
|
|
253
|
+
`when` if that matters.
|
|
254
|
+
|
|
255
|
+
Override per-binding with `allowIn()`:
|
|
256
|
+
|
|
257
|
+
```js
|
|
258
|
+
// Let the palette open from a focused checkbox or slider, but not from text fields
|
|
259
|
+
hotkeys.bind('mod+k', openPalette, null, {
|
|
260
|
+
allowIn: (e) => ['checkbox', 'radio', 'range'].includes(e.composedPath()[0].type),
|
|
261
|
+
})
|
|
262
|
+
```
|
|
216
263
|
|
|
217
264
|
### `init(options?)`
|
|
218
265
|
|
|
@@ -225,19 +272,26 @@ hotkeys.init({
|
|
|
225
272
|
})
|
|
226
273
|
```
|
|
227
274
|
|
|
228
|
-
Auto-initializes on `window` by default (browser environments)
|
|
275
|
+
Auto-initializes on `window` by default (browser environments): importing
|
|
276
|
+
`hotkey-router` attaches `keydown` and `keyup` listeners to `window` straight
|
|
277
|
+
away, and `hotkey-router/auto` also installs the reservation warnings. The
|
|
278
|
+
package's `sideEffects` list names those files so bundlers keep that work;
|
|
279
|
+
`/reservations`, `/sites`, `/validate` and `/format` do nothing at import and
|
|
280
|
+
tree-shake away when unused.
|
|
229
281
|
|
|
230
282
|
With `capture: false` (the default), bindings run in the bubble phase except
|
|
231
283
|
those bound with `{ capture: true }`, which a second, capture-phase listener
|
|
232
284
|
handles (v0.3.0+). `capture: true` here runs every binding in the capture
|
|
233
285
|
phase.
|
|
234
286
|
|
|
235
|
-
### `parseHotkey(hotkey)` / `comboFromEvent(event, options?)` (v0.3.0+)
|
|
287
|
+
### `parseHotkey(hotkey, options?)` / `comboFromEvent(event, options?)` (v0.3.0+)
|
|
236
288
|
|
|
237
289
|
Named exports (also on the default object). `comboFromEvent` turns a
|
|
238
290
|
keyboard event into a hotkey string for a "press your keys" recorder:
|
|
239
291
|
physical by default, `{ useMod: true }` writes the platform's primary
|
|
240
292
|
modifier as `mod`, and it returns `null` while only a modifier is held.
|
|
293
|
+
Both take `{ platform }` (v0.3.2+) to resolve `mod` for a platform other
|
|
294
|
+
than the one detected.
|
|
241
295
|
|
|
242
296
|
```js
|
|
243
297
|
import { comboFromEvent } from 'hotkey-router'
|
|
@@ -248,6 +302,14 @@ input.addEventListener('keydown', (e) => {
|
|
|
248
302
|
})
|
|
249
303
|
```
|
|
250
304
|
|
|
305
|
+
### `detectPlatform(navigator?, { chromeos? })` (v0.3.2+)
|
|
306
|
+
|
|
307
|
+
The one platform detector the router, reservations, sites, validate and
|
|
308
|
+
format all use: `'mac'`, `'windows'`, `'linux'` or `null`. ChromeOS counts as
|
|
309
|
+
`'linux'` unless you pass `{ chromeos: true }`. Reads
|
|
310
|
+
`navigator.userAgentData.platform`, then `navigator.platform`, then the user
|
|
311
|
+
agent. Also exported from `hotkey-router/reservations`.
|
|
312
|
+
|
|
251
313
|
### `destroy()`
|
|
252
314
|
|
|
253
315
|
Removes all listeners and clears internal state.
|
|
@@ -258,9 +320,14 @@ Programmatically trigger a hotkey. Useful for testing.
|
|
|
258
320
|
|
|
259
321
|
```js
|
|
260
322
|
hotkeys.trigger('ctrl+k')
|
|
323
|
+
hotkeys.trigger('ctrl+k', { target: inputElement }) // as if typed in the input
|
|
261
324
|
```
|
|
262
325
|
|
|
263
|
-
Returns `true` if a handler ran.
|
|
326
|
+
Returns `true` if a handler ran. Since v0.3.2 it fires what a real keypress
|
|
327
|
+
would: the synthetic event has both `key` and `code` (US layout), so
|
|
328
|
+
`trigger('ctrl+k')` also fires a `ctrl+code:KeyK` binding and the reverse,
|
|
329
|
+
and the same guards apply (`when`, `ignoreInput`/`allowIn` with `target`,
|
|
330
|
+
priority, `once`, `pause()`).
|
|
264
331
|
|
|
265
332
|
---
|
|
266
333
|
|
|
@@ -312,9 +379,9 @@ The `code:` value is **case-sensitive** (matches the camelCase `KeyboardEvent.co
|
|
|
312
379
|
|
|
313
380
|
Some combos are reserved by the browser chrome (find bar, devtools, bookmarks) or the OS (Spotlight, window management). They never reach page-world JavaScript, no matter how early you listen or whether you call `preventDefault`. hotkey-router ships an **opt-in** reservation table that emits a soft warning at bind time, so the silent failure becomes a noisy one.
|
|
314
381
|
|
|
315
|
-
The feature is opt-in by design: the core router
|
|
382
|
+
The feature is opt-in by design: the core router is ~10 KB minified (~4.5 KB gzipped), and the reservation module (~23 KB minified, ~5.5 KB gzipped, mostly data) is tree-shaken out entirely unless you import it.
|
|
316
383
|
|
|
317
|
-
**Two ways to opt in.** One-line ergonomic, use the `auto` entry. Same default export as `hotkey-router`, with warnings pre-installed
|
|
384
|
+
**Two ways to opt in.** One-line ergonomic, use the `auto` entry. Same default export as `hotkey-router`, with warnings pre-installed. It is the same router object, so other files can keep importing `hotkey-router` and their bindings are warned about too (v0.3.3+; earlier versions shipped a second copy of the router in `auto`). The CDN file `dist/auto.min.js` is a standalone build with its own router; don't mix it with other hotkey-router imports.
|
|
318
385
|
|
|
319
386
|
```js
|
|
320
387
|
import hotkeys from 'hotkey-router/auto'
|
|
@@ -428,8 +495,8 @@ shortcut runs.
|
|
|
428
495
|
Also exported: `lookupSiteConflicts(combo, { url, platform, verifiedOnly })`,
|
|
429
496
|
`matchSites(url)`, `siteLookupKey(combo, platform)`, `detectSitePlatform()`
|
|
430
497
|
and `listSites()`. Platforms: `windows` (also used for Linux), `mac`,
|
|
431
|
-
`chromeos`. The
|
|
432
|
-
core, so it never creates a second router.
|
|
498
|
+
`chromeos`. The module is ~38 KB minified (~9 KB gzipped, mostly the table)
|
|
499
|
+
and imports nothing from the core, so it never creates a second router.
|
|
433
500
|
|
|
434
501
|
### Validating a recorded combo (v0.3.0+)
|
|
435
502
|
|
|
@@ -461,6 +528,56 @@ already in `taken`, or reserved by a browser/OS (never reaches the page).
|
|
|
461
528
|
Warnings (`ok: true`): reserved only in some situations, Ctrl+Alt acting as
|
|
462
529
|
AltGr on some Windows layouts, and sites that use the same keys.
|
|
463
530
|
|
|
531
|
+
It loads both data tables: with them, about 62 KB minified (~15 KB
|
|
532
|
+
gzipped). Import it in the settings page that records keys, not in code
|
|
533
|
+
that runs on every page.
|
|
534
|
+
|
|
535
|
+
### Showing a combo to people (v0.3.2+)
|
|
536
|
+
|
|
537
|
+
`hotkey-router/format` turns a hotkey string (or a parsed combo) into what
|
|
538
|
+
people read. It loads no data tables (~9 KB minified, ~3.5 KB gzipped).
|
|
539
|
+
|
|
540
|
+
```js
|
|
541
|
+
import { formatCombo, comboTokens, modifierLabel } from 'hotkey-router/format'
|
|
542
|
+
|
|
543
|
+
formatCombo('mod+shift+code:Period') // 'Ctrl+Shift+.' (Windows/Linux)
|
|
544
|
+
formatCombo('mod+shift+code:Period', { platform: 'mac' }) // 'Shift+Cmd+.'
|
|
545
|
+
formatCombo('mod+shift+code:Period', { platform: 'mac', style: 'glyphs' }) // '⇧⌘.'
|
|
546
|
+
|
|
547
|
+
comboTokens('mod+shift+code:Period', { platform: 'mac' })
|
|
548
|
+
// [{ label: '⇧', name: 'Shift', modifier: 'shift' },
|
|
549
|
+
// { label: '⌘', name: 'Command', modifier: 'meta' },
|
|
550
|
+
// { label: '.', name: 'Period', key: 'Period' }]
|
|
551
|
+
|
|
552
|
+
modifierLabel('alt', { platform: 'mac', style: 'both' }) // '⌥ Option'
|
|
553
|
+
modifierLabel('mod') // 'Cmd' or 'Ctrl'
|
|
554
|
+
```
|
|
555
|
+
|
|
556
|
+
- `formatCombo(combo, { platform, style, order, separator })`: one string.
|
|
557
|
+
`style: 'text'` (default) joins words with `+`; `'glyphs'` uses the macOS
|
|
558
|
+
symbols (⌃ ⌥ ⇧ ⌘ ↩ ⌫ ⎋ ← …) with no separator.
|
|
559
|
+
- `comboTokens(combo, { platform, style, order })`: one keycap per key,
|
|
560
|
+
`'glyphs'` by default. `name` is what a screen reader should say; put it in
|
|
561
|
+
visually hidden text or `aria-label` next to a glyph.
|
|
562
|
+
- `modifierLabel(name, { platform, style })` (`'text'`, `'glyphs'` or
|
|
563
|
+
`'both'`), `modifierName(name, { platform })` (spoken), `keyLabel(key,
|
|
564
|
+
{ platform, style })`, `listModifiers(names, { platform, conjunction })`
|
|
565
|
+
(`'Ctrl, Alt or Shift'`), `platformLabel('mac')` (`'macOS'`),
|
|
566
|
+
`browserLabel('edge')`.
|
|
567
|
+
- `KEY_LABELS`: label and spoken name for `KeyboardEvent.code` values
|
|
568
|
+
(`Period` → `.` / "Period", `Enter` → Enter, Return on macOS, ↩).
|
|
569
|
+
|
|
570
|
+
Words per platform: macOS Cmd, Option, Control, Shift; Windows Ctrl, Alt,
|
|
571
|
+
Shift, Win; Linux the same with Super; ChromeOS with Search. `platform`
|
|
572
|
+
defaults to `detectPlatform()`. On Windows, Linux and ChromeOS `'glyphs'`
|
|
573
|
+
gives the same words as `'text'` (they have no modifier symbols).
|
|
574
|
+
|
|
575
|
+
**Modifier order.** Modifiers come in the platform's own order whatever order
|
|
576
|
+
the string is written in: on macOS Apple's ⌃⌥⇧⌘ (Control, Option, Shift,
|
|
577
|
+
Command), the order every Mac menu shows, so `mod+shift+.` is `⇧⌘.`; on
|
|
578
|
+
Windows, Linux and ChromeOS Win/Super/Search, Ctrl, Alt, Shift. Pass
|
|
579
|
+
`{ order: 'combo' }` to keep the written order (`⌘⇧.`).
|
|
580
|
+
|
|
464
581
|
### Supported syntax
|
|
465
582
|
|
|
466
583
|
**Standard:**
|
|
@@ -469,7 +586,8 @@ AltGr on some Windows layouts, and sites that use the same keys.
|
|
|
469
586
|
- `shift+a`
|
|
470
587
|
- `ctrl+k up`
|
|
471
588
|
- `mod+s` (meta on macOS, ctrl elsewhere)
|
|
472
|
-
- `ctrl++` or `ctrl+plus`
|
|
589
|
+
- `ctrl++` or `ctrl+plus` (the `+` key; reservation and site lookups treat
|
|
590
|
+
it as `=`, since `+` is Shift+= on most layouts)
|
|
473
591
|
|
|
474
592
|
**AHK-style prefix modifiers:**
|
|
475
593
|
|
|
@@ -485,7 +603,7 @@ Modifiers must appear before the base key.
|
|
|
485
603
|
|
|
486
604
|
- `ctrl`, `control`, `⌃`
|
|
487
605
|
- `shift`, `⇧`, `+`
|
|
488
|
-
- `alt`, `option`, `⌥`, `!`
|
|
606
|
+
- `alt`, `option`, `opt`, `⌥`, `!`
|
|
489
607
|
- `meta`, `cmd`, `command`, `win`, `⌘`, `#`
|
|
490
608
|
- `mod` (meta on macOS, ctrl elsewhere)
|
|
491
609
|
|