Calm motion: animations on this site only fade, with no movement
ShortcutRecorder
Press a key combo to record it
A field where the user presses the shortcut they want instead of typing its name: in a settings page for an editor, a player or the Command palette. The value is plain text like Ctrl+Shift+K. Show it elsewhere with Kbd.
Basic
Search
Save
Toggle sidebar
click a field, press the combo you want; Esc cancels, Backspace clears- Click the field or press Enter and it waits: held modifiers show as you press them, and the first other key completes the combo. Esc cancels, Backspace or Delete clears. Leaving the field stops recording.
- The combo is read from the physical key, so
Shift+1isShift+1and notShift+!. Modifiers are always in the order Ctrl, Alt, Shift, Meta. Arrow keys are↑ ↓ ← →, and a space isSpace. - A bare key such as
Gis refused with a message unlessallowBareis on. Function keys (F5) are always fine, and so are combos with Ctrl, Alt or Meta. takenlists combos that are already used: an array, or{ "Ctrl+K": "Open search" }so the message can say by what ("Ctrl+K is already used by Open search"). A taken combo is not set andconflict(combo, by)fires. The field's own current value is never a conflict.- Changes are announced to screen readers: set, cleared, canceled, refused.
- To act on a shortcut,
matchShortcut("Ctrl+K", event)in akeydownhandler.shortcutFromEvent(event)gives the combo for any event.
The browser keeps some combos for itself (Ctrl+W, Ctrl+T, Ctrl+N), and a page cannot capture them. Recording one can close the tab. Steer users away from them with taken.
Usage
ts
import { BlessShortcutRecorder, matchShortcut } from "blessing-ui";API
Props
| Name | Type | Default | Description |
|---|---|---|---|
taken | string[] | Record<string, string> | — | combos already taken, as a list or `{ "Ctrl+K": "Open search" }` to say by what |
allowBare | boolean | — | accept a bare key such as `G`; off asks for Ctrl, Alt or Meta, or a function key |
disabled | boolean | — | |
label | string | "Shortcut" | |
placeholder | string | "Not set" | |
labels | Partial<{ button: (label: string, combo: string) => string; pressKeys: string; recording: string; set: (combo: string) => string; needsModifier: (combo: string) => string; usedBy: (combo: string, by: string) => string; taken: (combo: string) => string; }> | — | button wording and the messages said while recording or on a refusal |
modelValue | string | "" |
Events
| Name | Payload | Description |
|---|---|---|
update:modelValue | [value: string] | |
conflict | [combo: string, by: string | undefined] |
Exposed
| Name | Type | Description |
|---|---|---|
start | () => void | |
focus | () => void |