This is an advanced guide. Inside Preact components, useHotkey and useHotkeys handle registration, cleanup, provider defaults, scopes, and fresh callbacks automatically. Use HotkeyManager directly only outside Preact components, such as inside third-party chart or canvas wrappers.
All hotkeys on a page share a single HotkeyManager instance that owns the DOM event listeners, matches key presses, tracks sequence progress, and evaluates scopes. Access it with getHotkeyManager():
import { getHotkeyManager } from '@tanstack/preact-hotkeys'
const manager = getHotkeyManager()manager.register accepts the same hotkeys and options as useHotkey and returns a registration handle:
const handle = manager.register('Mod+K', () => openCommandPalette())
handle.setOptions({ enabled: false }) // Merges updated options
handle.callback = () => openSearch() // Updates callback in place
handle.unregister() // Removes the registrationKeep a few differences from useHotkey in mind:
For example, wiring shortcuts inside a vanilla chart plugin:
import { getHotkeyManager } from '@tanstack/preact-hotkeys'
export function attachChartHotkeys(chart: Chart, element: HTMLElement) {
const manager = getHotkeyManager()
const handles = [
manager.register('I', () => chart.zoomIn(), { target: element }),
manager.register('O', () => chart.zoomOut(), { target: element }),
]
return () => handles.forEach((handle) => handle.unregister())
}Inside components, subscribe to registrations with useHotkeyRegistrations (see List the hotkeys on screen). Outside Preact, query the manager directly:
manager.getRegistrationCount() // Total registered hotkeys
manager.isRegistered('Mod+S') // Whether any registration matches
manager.isRegistered('Escape', panelElement) // Whether one matches on that targetisRegistered uses canonical matching just like duplicate detection, so 'Meta+S' matches a 'Mod+S' registration on macOS.
manager.registrations reads and subscribes like a TanStack Store holding a Map of registrations by ID:
const subscription = manager.registrations.subscribe((registrations) => {
console.log(`${registrations.size} hotkeys registered`)
})
subscription.unsubscribe()Because this store updates whenever a hotkey fires or a sequence step advances, keep subscribers lightweight. Raw registrations include callback. For the callback-free shape returned by useHotkeyRegistrations, pass a registration to toHotkeyRegistrationView.
Inside Preact, define scopes with HotkeysProvider (see HotkeysProvider & Scopes). For non-Preact UI, such as a dialog mounted by an imperative library, activate a scope directly on the manager and pass that scope to its registrations:
const releaseScope = manager.activateScope('image-viewer', { exclusive: true })
const close = manager.register('Escape', () => viewer.close(), {
scope: 'image-viewer',
})
// Clean up when the viewer closes
close.unregister()
releaseScope()activateScope returns a release function, and a scope remains active until every activation of it has been released. It accepts two options:
Call manager.isScopeActive(scope) to check whether a scope can currently fire, or inspect each registration's status (see List the hotkeys on screen).
manager.scopes is a store with four fields:
Inside Preact components, read the same state reactively with useHotkeyScopesState:
import { useHotkeyScopesState } from '@tanstack/preact-hotkeys'
function KeyboardOwner() {
const scopes = useHotkeyScopesState()
return scopes.exclusive ? <p>Shortcuts belong to {scopes.exclusive}</p> : null
}The devtools display the same live scope state without writing code.
Call manager.resetSequenceProgress() to clear partial progress on all active sequences, such as when an external UI action cancels a pending command.
To match a sequence inside your own keydown listener without registering on HotkeyManager, use createSequenceMatcher:
import { createSequenceMatcher } from '@tanstack/preact-hotkeys'
const matcher = createSequenceMatcher(['G', 'G'], { timeout: 1000 })
document.addEventListener('keydown', (event) => {
if (matcher.match(event)) scrollToTop()
})The matcher tracks its own state via getProgress() and reset(). Standalone matchers don't check ignoreInputs, call preventDefault(), or evaluate scopes, so handle those in your listener if needed.
For resetting HotkeyManager and KeyStateTracker between tests and pinning the platform, see Testing.
For resetting HotkeyManager and KeyStateTracker between tests and pinning the platform, see Testing.
See the HotkeyManager reference for the full method list.