TanStack
Hotkeys API Reference

AlpineHotkeys

Defined in: types.ts:99

Registrations and reactive state owned by one Alpine component.

Properties

createAppHotkeyList

ts
createAppHotkeyList: () => AlpineHotkeyState<RegisteredAppHotkeyListItem[]>;

Defined in: types.ts:273

Read every app hotkey with its live status through .value.

Returns

AlpineHotkeyState<RegisteredAppHotkeyListItem[]>


createAppHotkeys

ts
createAppHotkeys: () => AlpineHotkeyState<Readonly<Record<string, AppHotkey<string, HotkeyChord | HotkeySequence | null>>>>;

Defined in: types.ts:271

Read the effective app hotkeys from provideAppHotkeys through .value.

Returns

AlpineHotkeyState<Readonly<Record<string, AppHotkey<string, HotkeyChord | HotkeySequence | null>>>>


createCustomHotkeys

ts
createCustomHotkeys: () => AlpineCustomHotkeys;

Defined in: types.ts:277

Read and change the user's custom hotkeys.

Returns

AlpineCustomHotkeys


createFormatForAria

ts
createFormatForAria: () => (hotkey, options) => string | undefined;

Defined in: types.ts:237

Returns formatForAria with the scope's platform as the default, or the detected platform without one. Use its value for aria-keyshortcuts, so screen readers announce the shortcut. It returns undefined for a multi-step sequence, no hotkey, or a hotkey that can't fire right now (one that is disabled, in a suspended scope, a suspended character key, or not registered), so x-bind omits the attribute. An explicit platform option wins.

Physical bindings such as [KeyQ] name the key the user presses in Chromium browsers, once the keyboard layout has loaded, the same way as createFormatForDisplay. A layoutMap passed to the returned function wins.

Create it once, then call the returned function anywhere.

Returns

ts
(hotkey, options?): string | undefined;

Formats a hotkey for an aria-keyshortcuts attribute so screen readers can announce the shortcut. Mod+K becomes Meta+K on macOS and Control+K elsewhere.

Modifiers use the ARIA names (Control, Alt, Shift, Meta) and come before the key. Keys use KeyboardEvent.key values, such as K, /, Escape, or ArrowUp, with Space for the space bar and Plus for the plus key. A physical binding names the key at that position, so [KeyQ] becomes Q, or the key from layoutMap such as A on a French AZERTY keyboard. Logical punctuation stays as written, so '?' stays ? even where it takes Shift to type.

Returns undefined when the hotkey has no ARIA form, so the attribute is omitted. This applies to empty or null hotkeys, multi-step sequences, standalone modifiers, and physical codes with no key value such as [NumpadMemoryAdd]. A one-step sequence formats like its chord.

For a control with two shortcuts, join both formatted values with a space. In a server-rendered app, use your adapter's formatter or pass platform. Without a platform option, formatForAria uses 'linux' on the server, like formatForDisplay.

Parameters
hotkey

DisplayHotkey | null | undefined

A hotkey string, raw object, parsed chord, or nothing

options?

FormatAriaOptions = {}

The platform and keyboard layout

Returns

string | undefined

The attribute value, or undefined when there is none

Example
ts
formatForAria('Mod+K', { platform: 'mac' }) // 'Meta+K'
formatForAria('Mod+Shift+P', { platform: 'windows' }) // 'Control+Shift+P'
formatForAria('Shift+Space') // 'Shift+Space'
formatForAria(['G', 'H']) // undefined

Example

ts
Alpine.data('searchButton', () => {
  const hotkeys = createHotkeysScope()
  const formatForAria = hotkeys.createFormatForAria()

  return {
    get shortcut() {
      return formatForAria('Mod+K')
    },
    destroy() {
      hotkeys.destroy()
    },
  }
})
html
<button x-data="searchButton" :aria-keyshortcuts="shortcut">Search</button>

createFormatForDisplay

ts
createFormatForDisplay: () => {
  (hotkey, options): string[][];
  (hotkey, options): string[];
  (hotkey, options?): string;
  (hotkey, options): string | string[] | string[][];
};

Defined in: types.ts:199

Returns formatForDisplay with the scope's platform as the default, or the detected platform without one. A platform passed to the returned function wins.

Physical bindings such as [KeyQ] are labeled with the character the key types for this user in Chromium browsers, once the keyboard layout has loaded. A getter or an x-text expression that calls the function runs again when the layout arrives. See createKeyboardLayout. A layoutMap passed to the returned function wins. To never ask the browser for the layout, set keyboardLayout: false in the scope's default options. Firefox and Safari report no layout, so labels there keep the key's US character.

Create it once, then call the returned function anywhere.

Returns

ts
(hotkey, options): string[][];

Formats a hotkey for display in a user interface.

In a server-rendered app, format labels with your adapter's formatter, such as useFormatForDisplay, or pass platform. Called directly, formatForDisplay never reads a provider's platform. Without a platform option, it renders Linux labels on the server, so Mac users get a different label after hydration.

On macOS, uses symbols (⇧⌘S) in Control, Option, Shift, Command order. Display ordering does not change the normalized hotkey used for registration. On Windows/Linux, uses text (Ctrl+Shift+S) with + separators. The separator can be customized with separatorToken. Physical codes use conventional display labels: [KeyS] becomes S and [Digit2] becomes 2. Keys with no US key use their keycap legends: [IntlBackslash] and [IntlRo] become \ and [IntlYen] becomes ¥. These labels do not infer the active keyboard layout or change matching. Supply a resolved layoutMap for layout-aware labels; keyLabels overrides both layout entries and fallback labels. Sequence steps are formatted individually and joined with sequenceSeparator (a space by default).

A key reported by a KeyboardEvent, such as a held key from KeyStateTracker, can be outside the key types: pass it as { key }.

Parameters
hotkey

readonly DisplayHotkeyChord[]

A logical or physical hotkey string, raw object, ParsedHotkeyChord, or sequence

options

FormatDisplayOptions & object

Formatting options

Returns

string[][]

Display text, or individual key labels when parts is true (one array per step for sequences)

Example
ts
formatForDisplay('Mod+Shift+S', { platform: 'mac' })
// Returns: '⇧ ⌘ S' (symbols separated by spaces on macOS)

formatForDisplay('Mod+Shift+S', { platform: 'windows' })
// Returns: 'Ctrl+Shift+S'

formatForDisplay('Escape')
// Returns: 'Esc' (on all platforms)

formatForDisplay('Shift', { platform: 'mac' })
// Returns: '⇧'

formatForDisplay({ key: heldKey })
// A key from a KeyboardEvent, such as a held key

formatForDisplay(['G', 'H'])
// Returns: 'G H'
ts
(hotkey, options): string[];

Formats a hotkey for display in a user interface.

In a server-rendered app, format labels with your adapter's formatter, such as useFormatForDisplay, or pass platform. Called directly, formatForDisplay never reads a provider's platform. Without a platform option, it renders Linux labels on the server, so Mac users get a different label after hydration.

On macOS, uses symbols (⇧⌘S) in Control, Option, Shift, Command order. Display ordering does not change the normalized hotkey used for registration. On Windows/Linux, uses text (Ctrl+Shift+S) with + separators. The separator can be customized with separatorToken. Physical codes use conventional display labels: [KeyS] becomes S and [Digit2] becomes 2. Keys with no US key use their keycap legends: [IntlBackslash] and [IntlRo] become \ and [IntlYen] becomes ¥. These labels do not infer the active keyboard layout or change matching. Supply a resolved layoutMap for layout-aware labels; keyLabels overrides both layout entries and fallback labels. Sequence steps are formatted individually and joined with sequenceSeparator (a space by default).

A key reported by a KeyboardEvent, such as a held key from KeyStateTracker, can be outside the key types: pass it as { key }.

Parameters
hotkey

DisplayHotkeyChord

A logical or physical hotkey string, raw object, ParsedHotkeyChord, or sequence

options

FormatDisplayOptions & object

Formatting options

Returns

string[]

Display text, or individual key labels when parts is true (one array per step for sequences)

Example
ts
formatForDisplay('Mod+Shift+S', { platform: 'mac' })
// Returns: '⇧ ⌘ S' (symbols separated by spaces on macOS)

formatForDisplay('Mod+Shift+S', { platform: 'windows' })
// Returns: 'Ctrl+Shift+S'

formatForDisplay('Escape')
// Returns: 'Esc' (on all platforms)

formatForDisplay('Shift', { platform: 'mac' })
// Returns: '⇧'

formatForDisplay({ key: heldKey })
// A key from a KeyboardEvent, such as a held key

formatForDisplay(['G', 'H'])
// Returns: 'G H'
ts
(hotkey, options?): string;

Formats a hotkey for display in a user interface.

In a server-rendered app, format labels with your adapter's formatter, such as useFormatForDisplay, or pass platform. Called directly, formatForDisplay never reads a provider's platform. Without a platform option, it renders Linux labels on the server, so Mac users get a different label after hydration.

On macOS, uses symbols (⇧⌘S) in Control, Option, Shift, Command order. Display ordering does not change the normalized hotkey used for registration. On Windows/Linux, uses text (Ctrl+Shift+S) with + separators. The separator can be customized with separatorToken. Physical codes use conventional display labels: [KeyS] becomes S and [Digit2] becomes 2. Keys with no US key use their keycap legends: [IntlBackslash] and [IntlRo] become \ and [IntlYen] becomes ¥. These labels do not infer the active keyboard layout or change matching. Supply a resolved layoutMap for layout-aware labels; keyLabels overrides both layout entries and fallback labels. Sequence steps are formatted individually and joined with sequenceSeparator (a space by default).

A key reported by a KeyboardEvent, such as a held key from KeyStateTracker, can be outside the key types: pass it as { key }.

Parameters
hotkey

DisplayHotkey

A logical or physical hotkey string, raw object, ParsedHotkeyChord, or sequence

options?

FormatDisplayOptions & object

Formatting options

Returns

string

Display text, or individual key labels when parts is true (one array per step for sequences)

Example
ts
formatForDisplay('Mod+Shift+S', { platform: 'mac' })
// Returns: '⇧ ⌘ S' (symbols separated by spaces on macOS)

formatForDisplay('Mod+Shift+S', { platform: 'windows' })
// Returns: 'Ctrl+Shift+S'

formatForDisplay('Escape')
// Returns: 'Esc' (on all platforms)

formatForDisplay('Shift', { platform: 'mac' })
// Returns: '⇧'

formatForDisplay({ key: heldKey })
// A key from a KeyboardEvent, such as a held key

formatForDisplay(['G', 'H'])
// Returns: 'G H'
ts
(hotkey, options): string | string[] | string[][];

Formats a hotkey for display in a user interface.

In a server-rendered app, format labels with your adapter's formatter, such as useFormatForDisplay, or pass platform. Called directly, formatForDisplay never reads a provider's platform. Without a platform option, it renders Linux labels on the server, so Mac users get a different label after hydration.

On macOS, uses symbols (⇧⌘S) in Control, Option, Shift, Command order. Display ordering does not change the normalized hotkey used for registration. On Windows/Linux, uses text (Ctrl+Shift+S) with + separators. The separator can be customized with separatorToken. Physical codes use conventional display labels: [KeyS] becomes S and [Digit2] becomes 2. Keys with no US key use their keycap legends: [IntlBackslash] and [IntlRo] become \ and [IntlYen] becomes ¥. These labels do not infer the active keyboard layout or change matching. Supply a resolved layoutMap for layout-aware labels; keyLabels overrides both layout entries and fallback labels. Sequence steps are formatted individually and joined with sequenceSeparator (a space by default).

A key reported by a KeyboardEvent, such as a held key from KeyStateTracker, can be outside the key types: pass it as { key }.

Parameters
hotkey

DisplayHotkey

A logical or physical hotkey string, raw object, ParsedHotkeyChord, or sequence

options

FormatDisplayOptions

Formatting options

Returns

string | string[] | string[][]

Display text, or individual key labels when parts is true (one array per step for sequences)

Example
ts
formatForDisplay('Mod+Shift+S', { platform: 'mac' })
// Returns: '⇧ ⌘ S' (symbols separated by spaces on macOS)

formatForDisplay('Mod+Shift+S', { platform: 'windows' })
// Returns: 'Ctrl+Shift+S'

formatForDisplay('Escape')
// Returns: 'Esc' (on all platforms)

formatForDisplay('Shift', { platform: 'mac' })
// Returns: '⇧'

formatForDisplay({ key: heldKey })
// A key from a KeyboardEvent, such as a held key

formatForDisplay(['G', 'H'])
// Returns: 'G H'

Example

ts
Alpine.data('saveButton', () => {
  const hotkeys = createHotkeysScope()
  const formatForDisplay = hotkeys.createFormatForDisplay()

  return {
    get label() {
      return formatForDisplay('Mod+S')
    },
    destroy() {
      hotkeys.destroy()
    },
  }
})

createHeldKeyCodes

ts
createHeldKeyCodes: () => AlpineHotkeyState<Record<string, string>>;

Defined in: types.ts:114

Read the mapping of held logical names to physical codes through .value.

Returns

AlpineHotkeyState<Record<string, string>>


createHeldKeys

ts
createHeldKeys: () => AlpineHotkeyState<string[]>;

Defined in: types.ts:112

Read held logical key names through .value.

Returns

AlpineHotkeyState<string[]>


createHotkey

ts
createHotkey: AlpineCreateHotkey;

Defined in: types.ts:105

Register one hotkey: a chord such as 'Mod+S', a sequence such as ['G', 'G'], or an app hotkey. Getters track changing hotkeys and options. undefined, null, or '' registers nothing, for an optional shortcut.


createHotkeyHint

ts
createHotkeyHint: (hotkey, options?) => AlpineHotkeyState<boolean>;

Defined in: types.ts:159

Read whether held modifiers reveal a hotkey through .value. No hotkey (undefined, null, or '') is never visible.

Parameters

hotkey

MaybeGetter< | RegisterableHotkey | AppHotkey<string, HotkeyChord | HotkeySequence | null> | null | undefined>

options?

MaybeGetter<HeldModifierOptions>

Returns

AlpineHotkeyState<boolean>


createHotkeyRegistrations

ts
createHotkeyRegistrations: () => HotkeyRegistrationsResult;

Defined in: types.ts:164

Read live registrations, including disabled entries.

Returns

HotkeyRegistrationsResult


createHotkeys

ts
createHotkeys: (definitions, options?) => void;

Defined in: types.ts:107

Reconcile a list of hotkeys. Definition options override common options.

Parameters

definitions

MaybeGetter<readonly AlpineHotkeyDefinition[]>

options?

MaybeGetter<AlpineHotkeyCommonOptions>

Returns

void


createHotkeyScopesState

ts
createHotkeyScopesState: () => AlpineHotkeyState<HotkeyScopesState>;

Defined in: types.ts:166

Read the active, exclusive, allowed, and suspended scopes through .value.

Returns

AlpineHotkeyState<HotkeyScopesState>


createKeyboardLayout

ts
createKeyboardLayout: () => AlpineHotkeyState<KeyboardLayoutMap | null>;

Defined in: types.ts:269

Read the browser's keyboard layout through .value, or null until it loads and in browsers that don't report one. The layout maps each physical code to the character it types, such as KeyQ to a on a French AZERTY keyboard. Only Chromium browsers report one.

Creating it is what loads the layout. The layout is read again when the window regains focus, until the scope is destroyed. createFormatForDisplay uses it, so you only need it for labels you draw yourself.

To never ask the browser for the layout, set keyboardLayout: false in the scope's default options. .value is then null.

Returns

AlpineHotkeyState<KeyboardLayoutMap | null>

Example

ts
Alpine.data('moveKeys', () => {
  const hotkeys = createHotkeysScope()
  const layout = hotkeys.createKeyboardLayout()

  return {
    label(code: string) {
      return (layout.value?.get(code) ?? code).toUpperCase()
    },
    destroy() {
      hotkeys.destroy()
    },
  }
})

createKeyHold

ts
createKeyHold: (key) => AlpineHotkeyState<boolean>;

Defined in: types.ts:116

Read whether a key is held through .value.

Parameters

key

MaybeGetter<IndividualKey>

Returns

AlpineHotkeyState<boolean>


createKeyLockState

ts
createKeyLockState: (key) => AlpineHotkeyState<boolean | null>;

Defined in: types.ts:152

Read whether a lock key, such as Caps Lock, is on through .value: true when the lock is on, false when it is off, or null. Pass 'CapsLock', 'NumLock', or 'ScrollLock', or a getter.

Lock keys toggle instead of being held, so this reads the lock state that the browser reports on key and mouse events. .value is null until the user presses a key, clicks, or moves the mouse, after the window loses focus or the page is hidden, and always for Num Lock and Scroll Lock on macOS and iOS.

Parameters

key

MaybeGetter<LockKey>

Returns

AlpineHotkeyState<boolean | null>

Example

ts
Alpine.data('passwordField', () => {
  const hotkeys = createHotkeysScope()
  const capsLock = hotkeys.createKeyLockState('CapsLock')

  return {
    get capsLockOn() {
      return capsLock.value === true
    },
    destroy() {
      hotkeys.destroy()
    },
  }
})
html
<label x-data="passwordField">
  Password
  <input type="password" />
  <span x-show="capsLockOn" role="status">Caps Lock is on</span>
</label>

destroy

ts
destroy: () => void;

Defined in: types.ts:279

Release every registration, subscription, effect, and recorder. Idempotent.

Returns

void