hotkey-router 0.3.2 → 0.4.0
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 +157 -6
- package/dist/auto.js +5 -1101
- package/dist/auto.js.map +4 -4
- package/dist/auto.min.js +2 -2
- package/dist/core.js +697 -0
- package/dist/core.js.map +7 -0
- package/dist/format.js +1 -1
- package/dist/format.js.map +1 -1
- package/dist/hotkey-router.cjs +385 -266
- package/dist/hotkey-router.cjs.map +4 -4
- package/dist/hotkey-router.js +6 -574
- package/dist/hotkey-router.js.map +3 -3
- package/dist/hotkey-router.min.js +2 -2
- package/dist/reservations.js +1 -1
- package/dist/reservations.js.map +2 -2
- package/dist/sites.js +1 -1
- package/dist/sites.js.map +2 -2
- package/dist/types/core.d.ts +113 -0
- package/dist/types/hotkey-router.d.ts +20 -195
- package/dist/types/parse.d.ts +13 -0
- package/dist/types/validate.d.ts +2 -2
- package/dist/validate.js +193 -1
- package/dist/validate.js.map +3 -3
- package/package.json +17 -3
package/README.md
CHANGED
|
@@ -47,6 +47,16 @@ CDN (ESM):
|
|
|
47
47
|
import hotkeys from 'https://cdn.jsdelivr.net/npm/hotkey-router/dist/hotkey-router.min.js'
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
+
Nothing at import (v0.4.0+): `hotkey-router/core` has `createRouter` and the
|
|
51
|
+
helpers, but no ready router and no listeners until you start one.
|
|
52
|
+
|
|
53
|
+
```js
|
|
54
|
+
import { createRouter } from 'hotkey-router/core'
|
|
55
|
+
|
|
56
|
+
const keys = createRouter()
|
|
57
|
+
keys.init() // when your app is ready
|
|
58
|
+
```
|
|
59
|
+
|
|
50
60
|
---
|
|
51
61
|
|
|
52
62
|
## Quick start
|
|
@@ -126,6 +136,28 @@ a character (Windows reports AltGr as Ctrl+Alt, so `ctrl+alt+code:Comma`
|
|
|
126
136
|
would otherwise swallow the `<` a Polish or Czech user typed) and while an
|
|
127
137
|
IME is composing.
|
|
128
138
|
|
|
139
|
+
`when` and `allowIn` receive the `KeyboardEvent`, not an element. To check
|
|
140
|
+
where the key was typed, read `event.composedPath()[0]` (the element inside an
|
|
141
|
+
open shadow root) or `event.target`.
|
|
142
|
+
|
|
143
|
+
**Shifted punctuation.** With Shift held, `event.key` is the shifted
|
|
144
|
+
character, so a key-based `ctrl+shift+,` waits for a `,` that a US keyboard
|
|
145
|
+
reports as `<`, and never fires. Bind the physical key instead:
|
|
146
|
+
`ctrl+shift+code:Comma`. Since v0.3.3, `bind()` warns once in the console for
|
|
147
|
+
Shift with `` , . / ; ' [ ] \ - = ` ``, and `validateCombo` rejects it with the
|
|
148
|
+
reason `shifted-punctuation`.
|
|
149
|
+
|
|
150
|
+
**Non-Latin layouts (v0.3.3+).** On a Russian layout Ctrl+K reports
|
|
151
|
+
`event.key === 'л'` with `event.code === 'KeyK'`. Like the browser's own
|
|
152
|
+
shortcuts, a key-based `ctrl+k` binding still fires there: when Ctrl or Cmd is
|
|
153
|
+
held (Shift allowed, Alt not), the key types a letter from a non-Latin script
|
|
154
|
+
(Cyrillic, Greek, Hebrew, Arabic, ...), and `code` is `KeyA`..`KeyZ`, the
|
|
155
|
+
router also looks up the Latin letter for that key. It does this only when no
|
|
156
|
+
exact binding is eligible, so a `ctrl+л` or `ctrl+code:KeyK` binding always
|
|
157
|
+
wins over it. Plain and Shift-only letters (typing), Alt and AltGr chords, IME
|
|
158
|
+
composition, digits and Latin layouts such as AZERTY are never affected. To
|
|
159
|
+
require the exact character, gate the binding: `when: (e) => e.key === 'k'`.
|
|
160
|
+
|
|
129
161
|
**Examples**
|
|
130
162
|
|
|
131
163
|
Ignore repeat keydown (default behavior):
|
|
@@ -141,6 +173,11 @@ hotkeys.bind('mod+k', openPalette, null, {
|
|
|
141
173
|
allowIn: () => true,
|
|
142
174
|
preventDefault: true,
|
|
143
175
|
})
|
|
176
|
+
|
|
177
|
+
// Only in one field (the event, not the element, is passed in)
|
|
178
|
+
hotkeys.bind('enter', submitFollowUp, null, {
|
|
179
|
+
allowIn: (e) => e.composedPath()[0] === followUpInput,
|
|
180
|
+
})
|
|
144
181
|
```
|
|
145
182
|
|
|
146
183
|
Conditional binding:
|
|
@@ -181,6 +218,8 @@ unregister()
|
|
|
181
218
|
```
|
|
182
219
|
|
|
183
220
|
Plugin cleanup is isolated. Removing one plugin never affects other bindings.
|
|
221
|
+
If any key string fails to parse, `registerPlugin` throws and binds nothing
|
|
222
|
+
(v0.3.3+).
|
|
184
223
|
|
|
185
224
|
### `unregisterPlugin(name)`
|
|
186
225
|
|
|
@@ -203,9 +242,10 @@ Hook errors are caught and logged via `console.error`. A buggy hook can't break
|
|
|
203
242
|
|
|
204
243
|
Temporarily disable or re-enable all routing.
|
|
205
244
|
|
|
206
|
-
### `ignoreInput(
|
|
245
|
+
### `ignoreInput(value = true)`
|
|
207
246
|
|
|
208
|
-
|
|
247
|
+
`value` is `true` / `'all'` (the default, below), `'text'` (v0.4.0+, below)
|
|
248
|
+
or `false` (no check). By default, hotkeys do **not** fire inside:
|
|
209
249
|
|
|
210
250
|
- `<input>`
|
|
211
251
|
- `<textarea>`
|
|
@@ -213,7 +253,64 @@ By default, hotkeys do **not** fire inside:
|
|
|
213
253
|
- `[contenteditable]`
|
|
214
254
|
- `role="textbox"`
|
|
215
255
|
|
|
216
|
-
|
|
256
|
+
This includes those elements inside an open shadow root (v0.3.3+; the router
|
|
257
|
+
reads `event.composedPath()[0]`). A closed shadow root hides its inside from
|
|
258
|
+
the page, so only its host element is checked there.
|
|
259
|
+
|
|
260
|
+
Every `<input>` counts, whatever its `type` and whatever modifiers are held:
|
|
261
|
+
a checkbox, radio, range slider, color or file input blocks `ctrl+k` as well
|
|
262
|
+
as Space. A native `<button>` is **not** on the list, so a plain `space` or
|
|
263
|
+
`enter` binding fires while a button has focus; gate such bindings with
|
|
264
|
+
`when` if that matters.
|
|
265
|
+
|
|
266
|
+
Override per-binding with `allowIn()`:
|
|
267
|
+
|
|
268
|
+
```js
|
|
269
|
+
// Let the palette open from a focused checkbox or slider, but not from text fields
|
|
270
|
+
hotkeys.bind('mod+k', openPalette, null, {
|
|
271
|
+
allowIn: (e) => ['checkbox', 'radio', 'range'].includes(e.composedPath()[0].type),
|
|
272
|
+
})
|
|
273
|
+
```
|
|
274
|
+
|
|
275
|
+
**The `'text'` mode (v0.4.0+, opt-in).** For apps with forms and settings
|
|
276
|
+
panels, `hotkeys.ignoreInput('text')` (or `createRouter({ ignoreInput:
|
|
277
|
+
'text' })`) blocks typing but not shortcuts:
|
|
278
|
+
|
|
279
|
+
| Where the key was typed | Plain and Shift-only keys | Chords with Ctrl, Alt or Meta |
|
|
280
|
+
| --- | --- | --- |
|
|
281
|
+
| Text entry: text-like and date/time `<input>` types (and any type not listed below), `<textarea>`, `<select>`, contenteditable, `role="textbox"` | blocked | blocked |
|
|
282
|
+
| Other controls: checkbox, radio, range, color, file, button, submit, reset and image inputs; `<button>`; `role="button"`; any element inside a button | blocked (Space, Enter and arrows belong to the control) | **fire** |
|
|
283
|
+
|
|
284
|
+
A chord needs a non-modifier key, so bare-modifier bindings (`alt`,
|
|
285
|
+
`alt up`) stay blocked on controls. Where an element is both (an editable
|
|
286
|
+
element inside a button), text entry wins. `allowIn` still overrides the
|
|
287
|
+
input check, but not the AltGr, IME, repeat or `when` checks. The default
|
|
288
|
+
stays `'all'`, so nothing changes unless you opt in.
|
|
289
|
+
|
|
290
|
+
### `createRouter(options?)` (v0.4.0+)
|
|
291
|
+
|
|
292
|
+
A separate router: its own bindings, plugins, `pause()`, `ignoreInput()`,
|
|
293
|
+
`onBind` hooks and listeners, with the same methods as the default export.
|
|
294
|
+
It attaches nothing until you call `init()`.
|
|
295
|
+
|
|
296
|
+
```js
|
|
297
|
+
import { createRouter } from 'hotkey-router'
|
|
298
|
+
|
|
299
|
+
const dialogKeys = createRouter({ target: dialogEl })
|
|
300
|
+
dialogKeys.init()
|
|
301
|
+
dialogKeys.bind('escape', closeDialog)
|
|
302
|
+
// Later
|
|
303
|
+
dialogKeys.destroy() // the page's other routers keep working
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
Options: `target` (default `window` when there is one; an element, a
|
|
307
|
+
`ShadowRoot` or an iframe's window scopes the router to keys from inside
|
|
308
|
+
it), `capture` (default `false`) and `ignoreInput` (default `true`; also
|
|
309
|
+
`'text'` or `false`, as for `ignoreInput()`).
|
|
310
|
+
`target` and `capture` are defaults: `init({ target, capture })` overrides
|
|
311
|
+
them for that call only. `setTarget(target)` changes the default target of an
|
|
312
|
+
existing router, which is useful when other code holds on to it; it doesn't
|
|
313
|
+
move listeners until the next `init()`. See [Multiple routers](#multiple-routers-v040).
|
|
217
314
|
|
|
218
315
|
### `init(options?)`
|
|
219
316
|
|
|
@@ -226,7 +323,13 @@ hotkeys.init({
|
|
|
226
323
|
})
|
|
227
324
|
```
|
|
228
325
|
|
|
229
|
-
Auto-initializes on `window` by default (browser environments)
|
|
326
|
+
Auto-initializes on `window` by default (browser environments): importing
|
|
327
|
+
`hotkey-router` attaches `keydown` and `keyup` listeners to `window` straight
|
|
328
|
+
away, and `hotkey-router/auto` also installs the reservation warnings. The
|
|
329
|
+
package's `sideEffects` list names those files so bundlers keep that work;
|
|
330
|
+
`/core` (v0.4.0+), `/reservations`, `/sites`, `/validate` and `/format` do
|
|
331
|
+
nothing at import and tree-shake away when unused. `hotkey-router` is built
|
|
332
|
+
on `hotkey-router/core`, so an app that imports both bundles one copy.
|
|
230
333
|
|
|
231
334
|
With `capture: false` (the default), bindings run in the bubble phase except
|
|
232
335
|
those bound with `{ capture: true }`, which a second, capture-phase listener
|
|
@@ -298,6 +401,35 @@ hotkeys.bind('escape', closeModal, null, {
|
|
|
298
401
|
})
|
|
299
402
|
```
|
|
300
403
|
|
|
404
|
+
### Multiple routers (v0.4.0+)
|
|
405
|
+
|
|
406
|
+
The default export is one router, started on `window` at import. Each
|
|
407
|
+
`createRouter()` call makes another. Use one when:
|
|
408
|
+
|
|
409
|
+
- **two libraries in one bundle both use hotkey-router.** They'd otherwise share
|
|
410
|
+
the default router (one copy of the module, one registry), so one's
|
|
411
|
+
`pause()` or `destroy()` would stop the other's keys. With its own router
|
|
412
|
+
each is independent. A browser extension's content scripts don't share a
|
|
413
|
+
router with the page's own scripts anyway: they run in a separate,
|
|
414
|
+
isolated JavaScript world with their own copy of the module. Key events
|
|
415
|
+
still reach both.
|
|
416
|
+
- **keys belong to a scope.** A router on a dialog element, a `ShadowRoot` or
|
|
417
|
+
an iframe's window only sees keys from inside it, while page-wide keys stay
|
|
418
|
+
on the default router.
|
|
419
|
+
|
|
420
|
+
Routers don't arbitrate between each other. If two routers bind the same key
|
|
421
|
+
and the event reaches both, both fire: priority and recency only pick a
|
|
422
|
+
winner within one router. To let one take the key, gate the other with `when`,
|
|
423
|
+
or keep their scopes apart. `stopPropagation` doesn't stop another router
|
|
424
|
+
listening on the same target. `stopImmediatePropagation` does, but only for
|
|
425
|
+
routers whose listeners were added later.
|
|
426
|
+
|
|
427
|
+
Shared by every router: the parser, `comboFromEvent`, `detectPlatform`, and
|
|
428
|
+
the one-time console warning for shifted punctuation (once per combo per
|
|
429
|
+
loaded copy of the module). Reservation warnings are per router:
|
|
430
|
+
`installReservationWarnings(router)` covers that router's binds only, and
|
|
431
|
+
`hotkey-router/auto` covers the default router.
|
|
432
|
+
|
|
301
433
|
### Bare-modifier bindings (v0.1.0+)
|
|
302
434
|
|
|
303
435
|
Some UX patterns are driven by a bare modifier rather than a chord. For example, "hold Alt to enter select mode, release Alt to exit."
|
|
@@ -328,9 +460,9 @@ The `code:` value is **case-sensitive** (matches the camelCase `KeyboardEvent.co
|
|
|
328
460
|
|
|
329
461
|
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.
|
|
330
462
|
|
|
331
|
-
The feature is opt-in by design: the core router is ~
|
|
463
|
+
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.
|
|
332
464
|
|
|
333
|
-
**Two ways to opt in.** One-line ergonomic, use the `auto` entry. Same default export as `hotkey-router`, with warnings pre-installed
|
|
465
|
+
**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.
|
|
334
466
|
|
|
335
467
|
```js
|
|
336
468
|
import hotkeys from 'hotkey-router/auto'
|
|
@@ -601,6 +733,25 @@ Modern browsers supporting:
|
|
|
601
733
|
|
|
602
734
|
Chrome, Firefox, Safari, Edge.
|
|
603
735
|
|
|
736
|
+
### Testing
|
|
737
|
+
|
|
738
|
+
Everything runs locally; there is no hosted CI.
|
|
739
|
+
|
|
740
|
+
- `npm test`: unit tests in jsdom.
|
|
741
|
+
- `npm run test:browser` (v0.4.0+): builds, then runs `browser-tests/` in the
|
|
742
|
+
Chrome and Edge installed on this machine through `playwright-core`. That
|
|
743
|
+
covers real key events through shadow roots, routers scoped to a dialog, a
|
|
744
|
+
shadow root and an iframe, capture against a page that stops keys, the
|
|
745
|
+
`'text'` mode on real controls, and the built entries as an app bundles
|
|
746
|
+
them. Nothing is downloaded; a browser that isn't installed is skipped.
|
|
747
|
+
`BROWSERS=chrome` runs one. Needs Node 22 or later.
|
|
748
|
+
- `npm run data:age` (v0.4.0+): how old the browser/OS and website shortcut
|
|
749
|
+
tables are; warns past six months (the build prints the same warning).
|
|
750
|
+
[docs/data-review.md](docs/data-review.md) says how to review them.
|
|
751
|
+
- `scripts/key-check.ahk` (Windows, AutoHotkey 1.1): real keyboard layouts
|
|
752
|
+
(Russian, Greek, Hebrew, Polish AltGr) in Chrome, Firefox and Edge, with
|
|
753
|
+
results written to `docs/notes/`. See `playground/key-check.html`.
|
|
754
|
+
|
|
604
755
|
---
|
|
605
756
|
|
|
606
757
|
## License
|