Hotkeys Docs
Examples
- Kitchen Sink
- App Hotkeys
- Accessibility
- Scopes
- useHotkey
- useHotkey (sequences)
- useHotkey (platforms and alternates)
- useHotkeys
- useHotkeys (sequences)
- useHotkeyRecorder
- useHotkeyRecorder (sequences)
- useHeldKeys
- useKeyhold
- useKeyLockState
- useFormatForDisplay (Nuxt)
- useFormatForDisplay (Vite SSR)
- useFormatForDisplay (Astro)
- useFormatForDisplay (VitePress)
- Keyboard Layouts
Vue Example: Scopes
<script setup lang="ts">
import { TanStackDevtools } from '@tanstack/vue-devtools'
import {
HotkeysProvider,
useFormatForDisplay,
useHotkey,
useHotkeyScopesState,
} from '@tanstack/vue-hotkeys'
import { hotkeysDevtoolsPlugin } from '@tanstack/vue-hotkeys-devtools'
import { ref } from 'vue'
import EnabledScopes from './EnabledScopes.vue'
import ExclusiveScopes from './ExclusiveScopes.vue'
import NestedScopes from './NestedScopes.vue'
import Palette from './Palette.vue'
// Every hotkey in this example is a plain `useHotkey` call with no `scope`
// option. It registers in the scope of the nearest `HotkeysProvider` above
// its component that sets one, or in `global` when none does. A component
// reads the providers above it, not the ones it renders, so each scope's
// hotkeys live in a child component.
const formatForDisplay = useFormatForDisplay()
// Under the root provider in index.ts, which sets no scope, so it's in
// `global`.
const helpOpened = ref(0)
useHotkey('Alt+[KeyH]', () => helpOpened.value++)
// The hotkey manager's scope state, which updates as scopes change.
const scopes = useHotkeyScopesState()
// Sorted, because the order scopes activate in depends on the order the
// framework mounts components. `global` is always active and isn't listed.
const list = (names: ReadonlyArray<string>) =>
names.length > 0 ? [...names].sort().join(', ') : 'none'
</script>
<template>
<div class="app">
<header>
<h1>Hotkey scopes</h1>
<p>
Each <code>HotkeysProvider</code> with a <code>scope</code> groups the
hotkeys inside it. They fire while the provider is mounted and enabled,
and no exclusive scope suspends them.
</p>
</header>
<main>
<section class="demo-section">
<h2>Global scope</h2>
<p>
<kbd>{{ formatForDisplay('Alt+[KeyH]') }}</kbd> opens help. It's in
<code>global</code>, which is always active. Only an exclusive scope
suspends it.
</p>
<p role="status">
Help opened {{ helpOpened }} {{ helpOpened === 1 ? 'time' : 'times' }}
</p>
</section>
<HotkeysProvider scope="palette">
<Palette />
</HotkeysProvider>
<HotkeysProvider scope="document">
<NestedScopes />
</HotkeysProvider>
<HotkeysProvider scope="mail">
<EnabledScopes />
</HotkeysProvider>
<HotkeysProvider scope="settings">
<ExclusiveScopes />
</HotkeysProvider>
<section class="demo-section">
<h2>Scope state</h2>
<div role="status">
<p>Active: {{ list(scopes.active) }}</p>
<p>Exclusive: {{ scopes.exclusive ?? 'none' }}</p>
<p>Allowed: {{ list(scopes.allowed) }}</p>
<p>Suspended: {{ list(scopes.suspended) }}</p>
</div>
</section>
</main>
</div>
<TanStackDevtools :plugins="[hotkeysDevtoolsPlugin()]" />
</template>