Skip to content
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+1 is Shift+1 and not Shift+!. Modifiers are always in the order Ctrl, Alt, Shift, Meta. Arrow keys are ↑ ↓ ← →, and a space is Space.
  • A bare key such as G is refused with a message unless allowBare is on. Function keys (F5) are always fine, and so are combos with Ctrl, Alt or Meta.
  • taken lists 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 and conflict(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 a keydown handler. 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

NameTypeDefaultDescription
takenstring[] | Record<string, string>—combos already taken, as a list or `{ "Ctrl+K": "Open search" }` to say by what
allowBareboolean—accept a bare key such as `G`; off asks for Ctrl, Alt or Meta, or a function key
disabledboolean—
labelstring"Shortcut"
placeholderstring"Not set"
labelsPartial<{ 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
modelValuestring""

Events

NamePayloadDescription
update:modelValue[value: string]
conflict[combo: string, by: string | undefined]

Exposed

NameTypeDescription
start() => void
focus() => void

Released under the MIT License. Character names and artwork belong to their owners.