---
name: app-hotkeys
description: >-
  Use when an app built with TanStack Hotkeys needs a keyboard shortcut cheat
  sheet or help dialog, a command palette whose commands may have shortcuts, a
  settings screen where users rebind, unbind, or reset shortcuts, recording the
  shortcut a user presses, conflict checks between shortcuts, or saving custom
  shortcuts to storage.
metadata:
  purpose: >-
    Declare every shortcut once with defineAppHotkeys, then build cheat sheets,
    rebinding, conflict checks, and persistence from those declarations instead
    of a parallel shortcut registry.
  type: core
  library: '@tanstack/hotkeys'
  library_version: '0.16.0'
sources:
  - src/app-hotkeys.ts
  - src/hotkey-recorder.ts
  - src/conflicts.ts
  - TanStack/hotkeys:docs/framework/react/guides/app-hotkeys.md
  - TanStack/hotkeys:docs/framework/react/guides/cheat-sheet-ui-helpers.md
  - TanStack/hotkeys:docs/framework/react/guides/custom-hotkey-recording.md
  - 'TanStack/hotkeys:packages/react-hotkeys/tests/skills/shortcut-settings/**'
requires:
  - hotkeys-core
---

# App hotkeys

This skill builds on `hotkeys-core`. The adapter skill's app hotkeys reference shows the wiring in your framework.

App hotkeys are the library's shortcut catalog. Use them instead of a separate shortcut config, context, or hand-written cheat sheet. One module declares every shortcut. The root provider holds the user's custom hotkeys. Components register the declared app hotkeys with their callbacks. Plain hotkeys with `meta` are enough when a list only needs what is mounted right now.

```ts
import { defineAppHotkeys } from '@tanstack/hotkeys'

export const appHotkeys = defineAppHotkeys({
  'doc.save': { hotkey: 'Mod+S', meta: { name: 'Save' }, scope: 'document' },
  'comment.post': {
    hotkey: 'Mod+S',
    meta: { name: 'Post comment' },
    scope: 'comments',
  },
  'theme.toggle': { hotkey: null, meta: { name: 'Toggle theme' } },
})
```

Augment `Register` in the adapter module you import from, such as `declare module '@tanstack/react-hotkeys' { interface Register { appHotkeys: typeof appHotkeys } }`, to type ids. In React Server Components, such as a Next.js `app/layout.tsx`, a server component passes `appHotkeys` to `AppHotkeysProvider`, not `HotkeysProvider`, which throws there. Lit, Alpine, and Ember have no provider: call `provideAppHotkeys(appHotkeys, options)` once at startup. Pass saved data that loads later to `store.setOptions({ customHotkeys })` on the returned store; providers apply a changed `customHotkeys` prop themselves. `undefined` leaves custom hotkeys unchanged while data loads; `null` clears them, such as on sign-out.

## Pitfalls

- **Register the app hotkey, not its value.** Pass `appHotkeys['doc.save']`. Passing `.hotkey` registers the declared default, so the user's rebinding never applies, and the list reports the shortcut as `'inactive'`.
- **Save what `onCustomHotkeysChange` receives.** It holds only the shortcuts the user changed, so users still get new defaults. Saving every effective hotkey freezes today's defaults into their data.
- **Ids are a storage contract.** Saved data is keyed by id, and unknown ids are silently dropped, so renaming an id discards users' customizations.
- **`setCustomHotkey` returns conflicts and changes nothing when there are any.** Check the result before reporting success, or reject the key press earlier by calling `findConflicts` in the recorder's `validate`. It also refuses a value that isn't a valid hotkey, such as `''`, and warns in development. Pass `null` to unbind.
- **Declare scopes on app hotkeys that share keys.** An app hotkey without a scope, or in `'global'`, conflicts with every other app hotkey on the same keys, so rebinding is rejected.
- **Conflict checks need the recorder's `context`.** Two hotkeys can be written differently and fire for the same key press, such as `Shift+[Slash]` recorded by code and a declared `?`. `findConflicts(id, hotkey, context)` and `setCustomHotkey(id, hotkey, context)` check with the key press when they get the `context` that `validate` and `onRecord` receive. Without it, they only compare how the hotkeys are written.
- **A default can differ by platform.** Declare `hotkey: { default: 'Mod+Y', mac: 'Mod+Shift+Z' }`, with `null` for a platform without a default. `appHotkey.hotkey` and `defaultHotkey` then hold the object, not a string. Register and format the app hotkey as usual. To read the keys yourself, call `resolveHotkeys(appHotkey.hotkey, platform)`, which returns every hotkey for the platform with the one labels show first. `resolvePlatformHotkey` returns the platform's entry alone, without alternates. The object can list `alternates`, such as `{ default: 'Space', alternates: ['K'] }`. Each one that has a hotkey on the platform registers, and a duplicate registers once. A custom hotkey is still one chord or sequence, and it replaces the whole object, alternates included.
- **Rename a shared hotkey per page with call-site `meta`.** One declared `list.focus-search` can register as `{ meta: { name: 'Search tables' } }` on one page and `'Search roles'` on another. It merges over the declared meta for that registration, so registration lists and palettes show the page's name, while the settings row keeps the declared one. Don't declare a copy per page.
- **A command palette runs registrations, not declarations.** App hotkeys hold no callback. List the views from the adapter's registrations list and call `view.trigger()` on the one picked; it runs the callback the mounted component registered. It checks only `enabled`, so it still runs while the palette's exclusive scope suspends the hotkey.
- **Split a large catalog with nested `defineAppHotkeys` calls, not `satisfies`.** Each feature file calls `defineAppHotkeys`, and one module spreads the results into the object it registers and provides. Each call type-checks its own entries, and spread-in entries keep their id and default kind. `as const satisfies Record<string, AppHotkeyInput>` skips the kind-specific option check until the final call. A later spread replaces an entry with the same id silently, so keep ids unique across files.
- **A cheat sheet dialog usually gets an exclusive scope.** Register the hotkey that opened it inside the dialog too, so the same keys close it.

When building the "press a new shortcut" control, read [recording](references/recording.md).
