UtilitiesReact
A way to run your code when the user presses a key combination, from anywhere on the page or only while focus is inside an element.
Rendered by React 19.3.1
Not measured yetTests passing in React
3.4 KiBSize
Not measured yetMemory
Not measured yetSpeed
[N] commits
Installation
npm i @hamur/reactNot on npm yet. The package goes out under the chosen name.
Anatomy
Hotkeys has no parts. Import its functions from @hamur/react/hotkeys and call them.
Examples
Setting
A ? shortcut that opens a help panel, with a checkbox that turns it off and a field that remaps it, checked with isValidHotkey.
API reference
Hotkeys has no parts. It is 4 functions you call.
| Function | Description |
|---|---|
getHotkeyKeys | The tokens of each alternative in display order, Mod resolved for the detected platform. |
isValidHotkey | Whether hotkey is a valid hotkey string. |
matchesHotkey | Whether a keydown matches hotkey, apple defaulting to the detected platform. |
registerHotkey | Runs callback when hotkey is pressed; returns the function that removes it. |
Keyboard and accessibility
Keyboard
| Keys | Does | Part |
|---|---|---|
| ControlSMetaS | Runs the callback from anywhere on the page and prevents the browser's own | registerHotkey |
| ? | Runs the callback of a single-character shortcut, while the setting is on | registerHotkey |
Accessibility
| Part | Sets | So that |
|---|---|---|
| registerHotkey | aria-keyshortcuts on control, while the registration exists; no role or id | a screen reader can announce the shortcut of a control, and the attribute never outlives it |
Focus order
- Hotkeys is not focusable and takes no part in the tab order.
- A hotkey never moves the focus and never clicks the control; the callback decides what happens.