App hotkeys are optional. Plain createHotkey calls are enough until you need an app-wide cheat sheet or a shortcut settings screen. Because createHotkey registers shortcuts only while its component is mounted, a cheat sheet built purely from active registrations misses shortcuts on routes that haven't rendered and has no single store to edit them.
App hotkeys solve both. Declare every shortcut's ID, default binding, and metadata once with defineAppHotkeys, pass it to HotkeysProvider, and attach callbacks where the actions live.
Create a dedicated module for declarations without importing components or handlers, since this file loads with your app shell:
// app-hotkeys.ts
import { defineAppHotkeys } from '@tanstack/svelte-hotkeys'
export const appHotkeys = defineAppHotkeys({
'doc.save': { hotkey: 'Mod+S', meta: { name: 'Save', group: 'Document' } },
'doc.find': {
hotkey: 'Mod+F',
meta: { name: 'Find', group: 'Document' },
scope: 'editor',
},
'nav.home': {
hotkey: ['G', 'H'],
meta: { name: 'Go home', group: 'Navigation' },
options: { timeout: 800 },
},
'palette.open': { hotkey: null, meta: { name: 'Command palette' } },
})
declare module '@tanstack/svelte-hotkeys' {
interface Register {
appHotkeys: typeof appHotkeys
}
}The Register module augmentation enables autocomplete and type checking for getAppHotkeys(), getCustomHotkeys(), and CustomHotkeys.
Pass the declarations to the root HotkeysProvider. See Provide app hotkeys:
<script lang="ts">
import { HotkeysProvider } from '@tanstack/svelte-hotkeys'
import { appHotkeys } from './app-hotkeys'
import App from './App.svelte'
</script>
<HotkeysProvider {appHotkeys}>
<App />
</HotkeysProvider>Pass an app hotkey from getAppHotkeys().current inside a getter so the registration follows changes, or from a direct import of appHotkeys, anywhere createHotkey accepts a hotkey:
import { createHotkey, getAppHotkeys } from '@tanstack/svelte-hotkeys'
const appHotkeys = getAppHotkeys()
createHotkey(
() => appHotkeys.current['doc.save'],
() => save(),
() => ({ enabled: isDirty }),
)Inside HotkeysProvider, getAppHotkeys() and direct imports (such as createHotkey(appHotkeys['doc.save'], save)) both resolve to the user's current custom binding. Without a provider, direct imports fall back to the declared defaults, while getAppHotkeys() requires a provider with appHotkeys. When a user rebinds an app hotkey, active registrations update immediately, even when switching between a chord and a sequence.
Mix app hotkeys and regular hotkeys freely in createHotkeys:
createHotkeys(() => [
{ hotkey: appHotkeys.current['doc.save'], callback: save },
{ hotkey: appHotkeys.current['nav.home'], callback: () => goHome() },
{ hotkey: 'Escape', callback: close },
])getAppHotkeyList returns every declared app hotkey, even on routes that haven't rendered, along with its live registration status and isCustom flag. See List every app hotkey.
TanStack Hotkeys leaves storage up to your app. Pass saved overrides into customHotkeys and save updates when they change:
<script lang="ts">
import { HotkeysProvider } from '@tanstack/svelte-hotkeys'
import type { CustomHotkeys } from '@tanstack/svelte-hotkeys'
const saved: CustomHotkeys = JSON.parse(localStorage.getItem('hotkeys') ?? '{}')
</script>
<HotkeysProvider
{appHotkeys}
customHotkeys={saved}
onCustomHotkeysChange={(custom) =>
localStorage.setItem('hotkeys', JSON.stringify(custom))}
>
<App />
</HotkeysProvider>How incoming customHotkeys updates behave:
setHotkeysContext accepts the same appHotkeys, customHotkeys, and onCustomHotkeysChange options.
Because the hotkey recorder emits Mod rather than OS-specific Meta or Control, shortcuts saved on macOS carry over to Windows and Linux.
Use getCustomHotkeys alongside the hotkey recorder to build a shortcut settings row. See Hotkey Recording:
<script lang="ts">
import {
createHotkeyRecorder,
getAppHotkeys,
getCustomHotkeys,
getFormatForDisplay,
} from '@tanstack/svelte-hotkeys'
import type { AppHotkeyId } from '@tanstack/svelte-hotkeys'
let { id }: { id: AppHotkeyId } = $props()
const formatForDisplay = getFormatForDisplay()
const appHotkeys = getAppHotkeys()
const custom = getCustomHotkeys()
const appHotkey = $derived(appHotkeys.current[id])
const recorder = createHotkeyRecorder(() => ({
maxSteps: Infinity,
validate: (hotkey, context) => {
const conflicts = custom.findConflicts(id, hotkey, context)
return conflicts.length === 0 || `Used by ${conflicts[0]?.meta?.name}`
},
onRecord: (hotkey, context) => custom.setCustomHotkey(id, hotkey, context),
onClear: () => custom.setCustomHotkey(id, null),
}))
</script>
<div>
{appHotkey.meta?.name}:
{appHotkey.hotkey ? formatForDisplay(appHotkey.hotkey) : 'Unassigned'}
<button onclick={recorder.startRecording}>Change</button>
<button onclick={() => custom.resetCustomHotkey(id)}>Reset</button>
</div>Pass the recorder's context into findConflicts and setCustomHotkey so conflict detection compares the physical key presses too. That catches differently spelled bindings that fire for the same press, such as a recorded Shift+[Slash] against a declared ? on US keyboards.
| Member | Description |
|---|---|
| current | Current overrides that differ from the declared defaults |
| setCustomHotkey(id, hotkey, context?) | Assigns a hotkey, or unbinds with null. Returns any conflicting app hotkeys and changes nothing if there are any. |
| findConflicts(id, hotkey, context?) | Returns app hotkeys that collide with assigning hotkey to id |
| resetCustomHotkey(id) | Restores one app hotkey to its default |
| resetCustomHotkeys() | Restores all app hotkeys to their defaults |
Two app hotkeys conflict when their scopes overlap (matching scopes, or either is in 'global' or has no declared scope) and their bindings collide (equal chords, or one sequence starting with the other chord or sequence). Conflict checks scan every declaration, mounted or not.
Each app hotkey exposes defaultHotkey, and getAppHotkeyList includes isCustom to highlight modified rows. HotkeysProvider creates an AppHotkeysStore from the core package. For imperative use, see the AppHotkeysStore reference.
See the Kitchen Sink example for app hotkeys, grouped cheat sheets, and customization wired together.