Multi-key sequences
boundSequence requires several keys pressed in order within a timeout, like vim's d d. The first key does not fire immediately; the engine enters a pending state and waits for the rest. A timeout or a mismatched key cancels the sequence.
A sequence outranks plain bindings across elements inside one layer: even when a sibling element binds the sequence's first or next key, the sequence takes the key and the plain binding does not also fire. The outcome does not depend on applyElement order.
Prerequisites
| Chapter | Why you need it |
|---|---|
| Basic Binding | A sequence is a binding |
| Binding attribution | Sequences attribute exactly like boundKeyboard |
Signature
boundSequence(keys: string[], handler: KeyHandler, options?: SequenceOptions): () => void
boundSequence(sequenceActionId: string, options?: SequenceOptions): () => void
boundSequence(keys: string[], sequenceActionId: string, options?: SequenceOptions): () => voidkeys{string[]} The ordered keys; at least two. Required in the explicit-keys forms; not used by the preset-keys form.handler{() => void} Runs when the full sequence matches.sequenceActionId{string} A registeredSequenceOperationEntryID; itsactionis the handler and itskeys/timeoutact as defaults. See Shortcuts & Actions.options{SequenceOptions} AddstimeoutandexclusivetoBoundKeyboardOptions.- Returns: an unbind function.
const { boundSequence } = useKeyboard()
useEffect(
() => boundSequence(['d', 'd'], () => deleteItem()),
[boundSequence],
)
// With a registered sequence action
boundSequence('goto-top')
boundSequence(['ctrl+g', 'g'], 'goto-top', { timeout: 800 })boundSequence(['a'], ...) throws requires at least 2 keys in the sequence. An unknown sequenceActionId throws Action "<id>" is not registered.
Options
| Option | Type | Effect | Default |
|---|---|---|---|
timeout | number | Maximum gap between presses (ms) | 500 |
exclusive | boolean | Swallow a mismatched key and keep waiting; false cancels and releases it | false |
when | () => boolean | string | Condition | — |
mode | string | Restrict to a mode | — |
focusId | string | FocusRef | Scope to a focus target | — |
elementId | string | Scope to a layer element | injected |
stopsWorkingAfterLayerAppearing | boolean | Dormant while a layer is open | false |
boundSequence(['g', 'g'], () => jumpToTop(), { timeout: 700 })
boundSequence(['w', 'w'], () => lock(), { exclusive: true })times, once, and observer exist on the SequenceOptions type but the sequence matcher does not use them; they apply to boundKeyboard only.
Pending state
currentScreenHasSequenceWaiting(sync?)
thereGlobalQueueWaiting(sync?)currentScreenHasSequenceWaitingreads the caller's own scope: a page component sees the page's sequence, a layer or modal element sees that layer's sequence.thereGlobalQueueWaitingdoes the same forglobalSequence.- Both accept an optional
synccallback, invoked after every key press so a status line can re-render.
const { currentScreenHasSequenceWaiting } = useKeyboard()
const [, setTick] = useState(0)
const sync = useCallback(() => setTick((n) => n + 1), [])
const waiting = currentScreenHasSequenceWaiting(sync)Neither function consumes keys; they only report state.
Boundaries
- Every matched key resets
timeout. - With
exclusive: false, a mismatched key cancels the sequence and continues to its own bindings; withtrue, it is swallowed. - Sequences share attribution and focus rules with
boundKeyboard. - Return the unbind function before unmount so no pending sequence is left behind.
