|
| 1 | +--- |
| 2 | +title: Hotkeys |
| 3 | +description: Reactive keyboard shortcut management for Pyreon — scope-aware, modifier keys, lifecycle-managed |
| 4 | +--- |
| 5 | + |
| 6 | +# @pyreon/hotkeys |
| 7 | + |
| 8 | +Keyboard shortcut management with automatic lifecycle cleanup, scope-based activation, and modifier key support. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +```bash |
| 13 | +bun add @pyreon/hotkeys |
| 14 | +``` |
| 15 | + |
| 16 | +Peer dependencies: `@pyreon/core`, `@pyreon/reactivity` |
| 17 | + |
| 18 | +## Quick Start |
| 19 | + |
| 20 | +```tsx |
| 21 | +import { useHotkey } from '@pyreon/hotkeys' |
| 22 | + |
| 23 | +function Editor() { |
| 24 | + useHotkey('mod+s', () => save(), { description: 'Save document' }) |
| 25 | + useHotkey('mod+z', () => undo(), { description: 'Undo' }) |
| 26 | + useHotkey('mod+shift+z', () => redo(), { description: 'Redo' }) |
| 27 | + // Automatically unregistered when Editor unmounts |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +`mod` = ⌘ on Mac, Ctrl on Windows/Linux. |
| 32 | + |
| 33 | +## Component Hook — `useHotkey()` |
| 34 | + |
| 35 | +Registers a shortcut scoped to the component's lifecycle. Auto-unregisters on unmount. |
| 36 | + |
| 37 | +```tsx |
| 38 | +import { useHotkey } from '@pyreon/hotkeys' |
| 39 | + |
| 40 | +function App() { |
| 41 | + useHotkey('mod+k', () => openCommandPalette()) |
| 42 | + useHotkey('escape', () => closeModal()) |
| 43 | + useHotkey('ctrl+shift+p', () => openSettings(), { |
| 44 | + description: 'Open settings', |
| 45 | + preventDefault: true, |
| 46 | + }) |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +### Options |
| 51 | + |
| 52 | +| Option | Type | Default | Description | |
| 53 | +|--------|------|---------|-------------| |
| 54 | +| `scope` | `string` | `'global'` | Only fires when this scope is active | |
| 55 | +| `preventDefault` | `boolean` | `true` | Prevent default browser behavior | |
| 56 | +| `stopPropagation` | `boolean` | `false` | Stop event propagation | |
| 57 | +| `enableOnInputs` | `boolean` | `false` | Fire when input/textarea is focused | |
| 58 | +| `description` | `string` | — | For help dialogs | |
| 59 | +| `enabled` | `boolean \| () => boolean` | `true` | Dynamic enable/disable | |
| 60 | + |
| 61 | +## Scopes — `useHotkeyScope()` |
| 62 | + |
| 63 | +Scopes let you activate/deactivate groups of hotkeys based on UI context. |
| 64 | + |
| 65 | +```tsx |
| 66 | +import { useHotkey, useHotkeyScope } from '@pyreon/hotkeys' |
| 67 | + |
| 68 | +function Modal() { |
| 69 | + // Activate 'modal' scope while this component is mounted |
| 70 | + useHotkeyScope('modal') |
| 71 | + |
| 72 | + // This only fires when the modal scope is active |
| 73 | + useHotkey('escape', () => closeModal(), { scope: 'modal' }) |
| 74 | + useHotkey('enter', () => confirm(), { scope: 'modal' }) |
| 75 | +} |
| 76 | + |
| 77 | +function Editor() { |
| 78 | + useHotkeyScope('editor') |
| 79 | + |
| 80 | + useHotkey('ctrl+s', () => save(), { scope: 'editor' }) |
| 81 | + useHotkey('ctrl+z', () => undo(), { scope: 'editor' }) |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +The `global` scope is always active. Custom scopes activate when `useHotkeyScope()` mounts and deactivate when it unmounts. |
| 86 | + |
| 87 | +## Imperative API — `registerHotkey()` |
| 88 | + |
| 89 | +For use outside components (e.g., in stores or at app init): |
| 90 | + |
| 91 | +```tsx |
| 92 | +import { registerHotkey, enableScope, disableScope } from '@pyreon/hotkeys' |
| 93 | + |
| 94 | +// Returns an unregister function |
| 95 | +const unregister = registerHotkey('ctrl+s', () => save(), { |
| 96 | + description: 'Save', |
| 97 | +}) |
| 98 | + |
| 99 | +// Manual scope management |
| 100 | +enableScope('editor') |
| 101 | +disableScope('editor') |
| 102 | + |
| 103 | +// Later: cleanup |
| 104 | +unregister() |
| 105 | +``` |
| 106 | + |
| 107 | +## Modifier Keys |
| 108 | + |
| 109 | +| Modifier | Keys | |
| 110 | +|----------|------| |
| 111 | +| `ctrl` | `ctrl`, `control` | |
| 112 | +| `shift` | `shift` | |
| 113 | +| `alt` | `alt` | |
| 114 | +| `meta` | `meta`, `cmd`, `command` | |
| 115 | +| `mod` | ⌘ on Mac, Ctrl elsewhere | |
| 116 | + |
| 117 | +## Key Aliases |
| 118 | + |
| 119 | +| Alias | Key | |
| 120 | +|-------|-----| |
| 121 | +| `esc` | `Escape` | |
| 122 | +| `return` | `Enter` | |
| 123 | +| `del` | `Delete` | |
| 124 | +| `ins` | `Insert` | |
| 125 | +| `space` | ` ` (space) | |
| 126 | +| `up/down/left/right` | Arrow keys | |
| 127 | +| `plus` | `+` | |
| 128 | + |
| 129 | +## Input Filtering |
| 130 | + |
| 131 | +By default, hotkeys are **ignored** when the user is typing in: |
| 132 | +- `<input>` elements |
| 133 | +- `<textarea>` elements |
| 134 | +- `<select>` elements |
| 135 | +- `contentEditable` elements |
| 136 | + |
| 137 | +Override with `enableOnInputs: true`: |
| 138 | + |
| 139 | +```tsx |
| 140 | +// This fires even when typing in an input |
| 141 | +useHotkey('escape', () => blur(), { enableOnInputs: true }) |
| 142 | +``` |
| 143 | + |
| 144 | +## Dynamic Enable/Disable |
| 145 | + |
| 146 | +```tsx |
| 147 | +const canSave = computed(() => hasChanges() && !isSaving()) |
| 148 | + |
| 149 | +useHotkey('mod+s', () => save(), { |
| 150 | + enabled: () => canSave(), |
| 151 | + description: 'Save (only when changes exist)', |
| 152 | +}) |
| 153 | +``` |
| 154 | + |
| 155 | +## Help Dialogs |
| 156 | + |
| 157 | +Build keyboard shortcut help screens with `getRegisteredHotkeys()`: |
| 158 | + |
| 159 | +```tsx |
| 160 | +import { getRegisteredHotkeys, formatCombo, parseShortcut } from '@pyreon/hotkeys' |
| 161 | + |
| 162 | +function ShortcutHelp() { |
| 163 | + const hotkeys = getRegisteredHotkeys() |
| 164 | + |
| 165 | + return ( |
| 166 | + <table> |
| 167 | + <thead><tr><th>Shortcut</th><th>Description</th></tr></thead> |
| 168 | + <tbody> |
| 169 | + {hotkeys |
| 170 | + .filter(h => h.description) |
| 171 | + .map(h => ( |
| 172 | + <tr> |
| 173 | + <td><kbd>{formatCombo(parseShortcut(h.shortcut))}</kbd></td> |
| 174 | + <td>{h.description}</td> |
| 175 | + </tr> |
| 176 | + ))} |
| 177 | + </tbody> |
| 178 | + </table> |
| 179 | + ) |
| 180 | +} |
| 181 | +``` |
| 182 | + |
| 183 | +## Utilities |
| 184 | + |
| 185 | +```tsx |
| 186 | +import { parseShortcut, formatCombo, matchesCombo } from '@pyreon/hotkeys' |
| 187 | + |
| 188 | +// Parse a shortcut string into a KeyCombo |
| 189 | +const combo = parseShortcut('ctrl+shift+s') |
| 190 | +// { ctrl: true, shift: true, alt: false, meta: false, key: 's' } |
| 191 | + |
| 192 | +// Format back to human-readable |
| 193 | +formatCombo(combo) // 'Ctrl+Shift+S' |
| 194 | + |
| 195 | +// Check if a KeyboardEvent matches |
| 196 | +matchesCombo(event, combo) // true/false |
| 197 | +``` |
0 commit comments