Skip to content

Shortcuts & Actions ​

The action system separates "which key is pressed" from "which callback runs": register a callback under a unique actionId, then bind keys by that ID. Changing a keybinding never touches the callback, and changing the callback never requires re-binding.

Prerequisites ​

ChapterWhy you need it
Basic BindingThe other two boundKeyboard overloads

Action entry ​

ts
type ShortcutOperationEntry = {
  actionId: string
  action: () => void
  keys?: string[]
}
  • actionId is unique; bindings and triggers use it.
  • keys is an optional preset keybinding.
ts
const { defineShortcutAction, addAction } = useKeyboard()

defineShortcutAction([
  { actionId: 'save', action: () => save(), keys: ['s'] },
  { actionId: 'reset', action: () => reset(), keys: ['r'] },
])

addAction({ actionId: 'quit', action: () => process.exit(0) })
MethodReturnsEffect
defineShortcutAction(entries)booleanRegisters several atomically; false if any id already exists (nothing is registered)
addAction(entry)booleanRegisters one; false on a duplicate id
hasAction(actionId)booleanWhether it is registered
removeAction(actionId)booleanRemoves; false when unknown
modifyAction(actionId, keys)booleanChanges preset keys; false when unknown or registered without keys; affects later bindings only

The registry belongs to the current engine instance; useKeyboard() returns the one owned by the active KeyboardProvider.

Three binding forms ​

FormEffect
boundKeyboard(keys, handler, options?)Ordinary callback binding
boundKeyboard(actionId, options?)Bind using the action's preset keys
boundKeyboard(keys, actionId, options?)Bind using explicit keys, overriding presets
ts
boundKeyboard('save')            // preset keys of save
boundKeyboard(['x'], 'save')     // explicit key x
boundKeyboard(['t'], 'toggle')   // actions without preset keys need this form
  • boundKeyboard(actionId) requires the action to have been registered with keys, otherwise it throws does not have predefined keys.
  • Both forms throw when the action is unknown: [keyboard-engine] Action "<id>" is not registered.

Sequence actions ​

boundSequence uses a separate sequence-action registry:

ts
defineSequenceAction([{ sequenceActionId: 'goto-top', action: scrollTop, keys: ['g', 'g'] }])

boundSequence('goto-top')                    // preset keys
boundSequence(['ctrl+g', 'g'], 'goto-top')   // explicit keys override presets
MethodReturnsNotes
defineSequenceAction(entries)booleanAtomic; false if any id already exists
addSequenceAction(entry)booleanfalse on a duplicate id
hasSequenceAction(id)booleanWhether it is registered
removeSequenceAction(id)booleanfalse when unknown
modifySequenceAction(id, keys, timeout?)booleanfalse when unknown or without preset keys; timeout: 0 is honored
clearSequenceOperations()voidRemoves every sequence action

Shortcut-action IDs and sequence-action IDs are independent namespaces.

Shortcut action or sequence action? ​

NeedShortcut actionSequence action
One key triggers a callbackYes—
A key chain triggers a callback—Yes
Preset keyskeyskeys + optional timeout
Bind withboundKeyboardboundSequence
Register withdefineShortcutActiondefineSequenceAction

Both registries are per engine instance and independent. Use a shortcut action when the trigger is one key; use a sequence action when the trigger itself is a multi-key gesture.

Boundaries ​

  • One action can be referenced by several keys and bindings.
  • modifyAction does not rewrite bindings that already exist.
  • Exceptions thrown inside an action propagate; handle them in the callback when isolation is needed.
  • Return unbind functions for bindings; actions themselves are not removed automatically.
  • Module-level calls can register actions directly; see Module-level keyboard API.

Released under the MIT License.