App hotkeys are optional. Plain injectHotkey is enough until you need a cheat sheet for your whole app, or a screen where users change hotkeys. injectHotkey registers a hotkey while its component exists, so a cheat sheet built from live registrations misses hotkeys on routes that have not rendered yet, and a settings screen has nowhere to change them. App hotkeys solve both: you declare every hotkey in one place, pass the declarations to provideHotkeys, and attach callbacks where the work happens. Users can then customize any of them, and your app decides how to save their choices.
A declaration holds a hotkey's id, default hotkey, metadata, and options. It never holds a callback. injectHotkey and injectHotkeys still attach the callbacks.
Use defineAppHotkeys in a module that contains only declarations. Keep it free of components and handlers, because it loads with your app shell:
// app-hotkeys.ts
import { defineAppHotkeys } from '@tanstack/angular-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/angular-hotkeys' {
interface Register {
appHotkeys: typeof appHotkeys
}
}The Register augmentation types injectAppHotkeys(), the ids accepted by injectCustomHotkeys(), and the CustomHotkeys type.
Pass the declarations to provideHotkeys in your application config. See Provide app hotkeys:
import { provideHotkeys } from '@tanstack/angular-hotkeys'
import { appHotkeys } from './app-hotkeys'
bootstrapApplication(AppComponent, {
providers: [provideHotkeys({ appHotkeys })],
})Pass an app hotkey wherever you pass a hotkey. Read it from injectAppHotkeys(), which returns a signal, or import it directly:
import { Component, signal } from '@angular/core'
import { injectAppHotkeys, injectHotkey } from '@tanstack/angular-hotkeys'
@Component({ selector: 'app-editor', template: `...` })
export class EditorComponent {
readonly appHotkeys = injectAppHotkeys()
readonly isDirty = signal(false)
constructor() {
injectHotkey(() => this.appHotkeys()['doc.save'], () => this.save(), () => ({
enabled: this.isDirty(),
}))
}
}Both forms use the provider's effective hotkey for that id, so a user's custom hotkey applies even to a direct import, such as injectHotkey(appHotkeys['doc.save'], save). Without provideHotkeys, the declared default is used.
The registration takes meta and options from the declaration. Options merge in this order: provider defaultOptions, then declared options, then call-site options. meta comes only from the declaration, so the cheat sheet and the registration cannot disagree. Call-site options are typed by the declared default's kind, so timeout is accepted for nav.home but not for doc.save.
When a component registers several hotkeys, pass app hotkeys to injectHotkeys in the same field, mixed with plain hotkeys if you like:
injectHotkeys(() => [
{ hotkey: this.appHotkeys()['doc.save'], callback: () => this.save() },
{ hotkey: this.appHotkeys()['nav.home'], callback: () => this.goHome() },
{ hotkey: 'Escape', callback: () => this.close() },
])When the user changes a hotkey, registered app hotkeys move to the new hotkey immediately, even if it changes from a chord to a sequence.
injectAppHotkeyList returns every app hotkey, with its current hotkey and whether anything has registered it. Use it to build a cheat sheet that includes pages that haven't rendered. See List every app hotkey.
TanStack Hotkeys does not store anything. Pass the user's saved custom hotkeys to provideHotkeys and save them when they change:
import { provideHotkeys } from '@tanstack/angular-hotkeys'
bootstrapApplication(AppComponent, {
providers: [
provideHotkeys({
appHotkeys,
customHotkeys: JSON.parse(localStorage.getItem('hotkeys') ?? '{}'),
onCustomHotkeysChange: (custom) =>
localStorage.setItem('hotkeys', JSON.stringify(custom)),
}),
],
})The hotkey recorder records Mod rather than platform-specific modifiers, so a hotkey saved on one platform works on others.
injectCustomHotkeys reads and changes the user's custom hotkeys. Pair it with a recorder, so users press the hotkey they want:
import { Component, computed, input } from '@angular/core'
import {
formatForDisplay,
injectAppHotkeys,
injectCustomHotkeys,
injectHotkeyRecorder,
} from '@tanstack/angular-hotkeys'
import type { AppHotkeyId } from '@tanstack/angular-hotkeys'
@Component({
selector: 'app-shortcut-row',
template: `
{{ appHotkey().meta?.name }}:
{{ appHotkey().hotkey ? format(appHotkey().hotkey!) : 'Unassigned' }}
<button (click)="recorder.startRecording()">Change</button>
<button (click)="custom.resetCustomHotkey(id())">Reset</button>
`,
})
export class ShortcutRowComponent {
readonly id = input.required<AppHotkeyId>()
readonly appHotkeys = injectAppHotkeys()
readonly appHotkey = computed(() => this.appHotkeys()[this.id()])
readonly custom = injectCustomHotkeys()
readonly format = formatForDisplay
readonly recorder = injectHotkeyRecorder(() => ({
maxSteps: Infinity,
validate: (hotkey) => {
const conflicts = this.custom.findConflicts(this.id(), hotkey)
return conflicts.length === 0 || `Used by ${conflicts[0]?.meta?.name}`
},
onRecord: (hotkey) => this.custom.setCustomHotkey(this.id(), hotkey),
onClear: () => this.custom.setCustomHotkey(this.id(), null),
}))
}| Member | Description |
|---|---|
| customHotkeys() | The hotkeys that differ from the defaults |
| setCustomHotkey(id, hotkey) | Sets a hotkey, or unbinds with null. Returns conflicting app hotkeys; when there are any, nothing changes. |
| findConflicts(id, hotkey) | App hotkeys that would collide if id used hotkey |
| resetCustomHotkey(id) | Restores one default |
| resetCustomHotkeys() | Restores every default |
Two app hotkeys conflict when their scopes match or either is in 'global', including app hotkeys with no declared scope, and their hotkeys collide: equal chords, or one sequence starts with the other, including a sequence whose first step equals a chord. Conflicts are checked across every declaration, whether or not it is registered.
Each app hotkey's defaultHotkey holds its declared default, and isCustom in injectAppHotkeyList reports whether the effective hotkey differs from it.
provideHotkeys creates an AppHotkeysStore from the core package. See the AppHotkeysStore reference to use it directly.