HotkeysProvider is optional. Without one, every hotkey uses the built-in defaults and belongs to the 'global' scope, so it works whenever its component is mounted. Many apps never need more. Add a provider to:
Pass defaultOptions to change the defaults for everything inside the provider:
<script setup lang="ts">
import { HotkeysProvider } from '@tanstack/vue-hotkeys'
</script>
<template>
<HotkeysProvider
:default-options="{
hotkey: { requireReset: true, timeout: 1500 },
hotkeyRecorder: { idleTimeoutMs: 2000 },
}"
>
<App />
</HotkeysProvider>
</template>A nested provider merges its defaults over its parent's, so a provider deeper in the tree can change the defaults for one part of your app.
Pass your app hotkeys to the root provider, along with the user's saved custom hotkeys, and listen for changes to save them:
<template>
<HotkeysProvider
:app-hotkeys="appHotkeys"
:custom-hotkeys="savedHotkeys"
@custom-hotkeys-change="saveHotkeys"
>
<App />
</HotkeysProvider>
</template>Nested providers use the app hotkeys and custom hotkeys of the provider above them, so pass these only once, at the root.
Apps constantly reuse keys. Escape should close the active dialog rather than the panel behind it, and Mod+S in a comment box should post the comment instead of saving the document. Rather than passing enabled to every hotkey and coordinating across components, group regions of your app into scopes.
Scopes group hotkeys by where they render instead. Wrap part of your tree in a HotkeysProvider with a scope, and every hotkey registered inside it belongs to that scope. The scope is active while the provider is mounted and enabled, and nested scopes take precedence over the scopes around them.
<!-- App.vue -->
<script setup lang="ts">
import { HotkeysProvider } from '@tanstack/vue-hotkeys'
</script>
<template>
<HotkeysProvider>
<Toolbar />
<HotkeysProvider scope="editor">
<Editor />
</HotkeysProvider>
</HotkeysProvider>
</template><!-- Editor.vue -->
<script setup lang="ts">
import { useHotkey } from '@tanstack/vue-hotkeys'
// Registered in the 'editor' scope
useHotkey('Mod+B', () => toggleBold())
</script>A hotkey registered with useHotkey or useHotkeys uses the scope of the nearest provider that sets one. Hotkeys outside any scoped provider, including those under the root provider, are in the 'global' scope, which is always active. A provider without scope uses its parent's scope.
To give a component's children a scope without adding HotkeysProvider to its template, call provideHotkeysContext in its setup. It takes the same options as the provider's props, and scope, exclusive, and enabled accept refs and getters:
<!-- EditorPane.vue -->
<script setup lang="ts">
import { provideHotkeysContext } from '@tanstack/vue-hotkeys'
const props = defineProps<{ active: boolean }>()
provideHotkeysContext({ scope: 'editor', enabled: () => props.active })
</script>
<template>
<!-- Editor's hotkeys are in the 'editor' scope -->
<Editor />
</template>Like Vue's provide, it applies to the component's children, not to the component itself. A hotkey registered in the same setup as provideHotkeysContext uses the scope above it, so register the scope's hotkeys in a child component.
Scopes are names, not instances. Two mounted providers with scope="editor" share one scope, and it stays active until both unmount. Give each region its own name to turn them on and off separately.
A scope and a target both limit when a hotkey works, and a hotkey can have both:
<!-- Editor.vue, rendered inside <HotkeysProvider scope="editor"> -->
<script setup lang="ts">
import { ref } from 'vue'
import { useHotkey } from '@tanstack/vue-hotkeys'
const outline = ref<HTMLElement | null>(null)
// In the 'editor' scope: works wherever focus is, while the scope is active
useHotkey('Mod+B', () => toggleBold())
// Also has a target: works only while focus is inside the outline
useHotkey('Delete', () => deleteHeading(), { target: outline })
</script>
<template>
<nav ref="outline" tabindex="0">...</nav>
</template>| Scope | Target | |
|---|---|---|
| Controls | Whether the hotkey is active | Which key presses reach the hotkey |
| Driven by | Which providers are mounted and enabled | Where focus is |
| Follows | The component tree, including teleports | The DOM |
| Set with | HotkeysProvider scope, provideHotkeysContext, or the scope option | The target option |
| Use for | Dialogs, panes, and tabs | Focusable elements, such as a list or a code editor |
When several active scopes register the same chord, or a sequence that completes in more than one of them, only the highest-precedence scope runs:
Every matching hotkey in the winning scope runs, and hotkeys in other scopes do not.
<!-- DocumentView.vue, rendered inside <HotkeysProvider scope="document"> -->
<script setup lang="ts">
import { HotkeysProvider, useHotkey } from '@tanstack/vue-hotkeys'
useHotkey('Mod+S', () => saveDocument())
</script>
<template>
<HotkeysProvider scope="comments">
<!-- CommentBox registers Mod+S too, and wins while mounted -->
<CommentBox />
</HotkeysProvider>
</template>When the comment box unmounts, Mod+S saves the document again.
Precedence compares hotkeys on the same target. A hotkey bound to an element still handles events from inside that element before document hotkeys do, as long as its scope is active.
Two registrations of the same hotkey in different scopes are not a conflict, so conflictBehavior does not warn about them.
Bind enabled to deactivate a provider's scope while it stays mounted. This suits tabs, carousels, and split views that keep inactive panes rendered:
<script setup lang="ts">
import { ref } from 'vue'
import { HotkeysProvider } from '@tanstack/vue-hotkeys'
const current = ref('inbox')
</script>
<template>
<HotkeysProvider
v-for="name in folders"
:key="name"
:scope="name"
:enabled="current === name"
>
<!-- FolderPane calls useHotkey('Alt+N', ...). Only the current folder's handler runs. -->
<FolderPane :name="name" />
</HotkeysProvider>
</template>FolderPane doesn't know which folder is current. Without scopes, each pane would need an enabled option on every hotkey it registers.
enabled is inherited. Disabling a provider also deactivates every scope nested in it. A provider without its own scope has no scope to deactivate, so disabling it affects only the scoped providers inside it.
Hotkeys in an inactive scope stay registered. Devtools list them under their inactive scope, and useAppHotkeyList reports their app hotkeys as 'suspended'.
A page cached by <KeepAlive> stops its hotkeys while it is hidden. Its hotkeys unregister, and its providers release their scopes, so a cached dialog's exclusive scope doesn't block the visible page. When the page shows again, its hotkeys and scopes come back. Recorders on a hidden page stop recording.
This also applies to <RouterView> and <NuxtPage keepalive> pages that use <KeepAlive>.
An exclusive scope suspends every other scope while it is active, including 'global'. Use it for dialogs, menus, and other overlays that should own the keyboard:
<!-- EditDialog.vue -->
<script setup lang="ts">
import { HotkeysProvider } from '@tanstack/vue-hotkeys'
</script>
<template>
<HotkeysProvider scope="edit-dialog" exclusive>
<Teleport to="body">
<!-- DialogBody registers Escape to close and Mod+S to save -->
<DialogBody />
</Teleport>
</HotkeysProvider>
</template>While the dialog is open, Mod+S saves the dialog and nothing else, even though the page behind it registers Mod+S too. Scopes nested inside the exclusive provider keep working, so the dialog can still be built from scoped components.
A scope follows the component tree, not the DOM. The Teleport above renders into document.body, but its hotkeys still belong to edit-dialog.
Pass { allow } to keep some scopes working while an exclusive scope is active:
<HotkeysProvider
scope="edit-dialog"
:exclusive="{ allow: ['global', 'command-palette'] }"
>
<DialogBody />
</HotkeysProvider>When two exclusive scopes are active at once, such as a confirmation dialog opened from inside an edit dialog, the higher-precedence one wins and only its allow list applies. Give each exclusive scope a unique name. Activating the same exclusive scope twice logs a development warning, because hotkeys from both providers would fire together.
Pass scope to a single hotkey to register it somewhere other than the nearest provider's scope:
// Registered in 'global' even when this component renders inside a scope
useHotkey('Mod+K', () => openPalette(), { scope: 'global' })A hotkey can name any scope, but it fires only while some provider has that scope active.
An app hotkey can declare a scope. The declared scope wins over the provider it registers under:
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' },
'palette.open': { hotkey: 'Mod+K', meta: { name: 'Command palette' }, scope: 'global' },
})For UI that Vue doesn't render, such as a dialog from another library, activate a scope on the hotkey manager. See Activate a scope.
HotkeysProvider and provideHotkeysContext need a component, because they use provide and onMounted. A hotkey registered in an effectScope outside a component can't read a provider's defaults, scope, or app hotkeys. Pass scope in its options instead. See Choose a scope for one hotkey.
Pass suspend-character-keys to HotkeysProvider (or suspendCharacterKeys to provideHotkeysContext) to pause document-level character key shortcuts, such as K or ['G', 'H'], across the app while the value is true. Suspended hotkeys report the status 'suspended', like hotkeys in an inactive scope, and the option applies even when enabled is false. Wire it to a user setting as shown in Accessibility.
The devtools group registrations by scope and show which scopes are active, suspended, allowed, or exclusive. To read the same state in your own UI, see Read scope state.
See the Scopes example for nested, enabled, and exclusive scopes together.