Keyboard shortcuts help people who navigate without a mouse, but shortcuts can get in the way of other assistive technology. Screen readers can't announce a shortcut unless the control sets aria-keyshortcuts. Speech input types words as key presses, which can fire single-letter shortcuts by accident. This guide covers what TanStack Hotkeys handles by default and what to add in your app.
These protections are on by default:
Screen readers announce a control's shortcut from its aria-keyshortcuts attribute. A visible <kbd> label isn't enough. Screen readers may read a <kbd> label as plain text rather than as a shortcut, and they may not read a symbol such as ⌘ as Command. Call useFormatForAria() to format a hotkey for the attribute using the same platform and keyboard layout as the display formatter:
<script setup lang="ts">
import {
useAppHotkeys,
useFormatForAria,
useFormatForDisplay,
useHotkey,
} from '@tanstack/vue-hotkeys'
import { computed } from 'vue'
const emit = defineEmits<{ search: [] }>()
const appHotkeys = useAppHotkeys()
useHotkey(() => appHotkeys.value['search.open'], () => emit('search'))
const formatForAria = useFormatForAria()
const formatForDisplay = useFormatForDisplay()
const hotkey = computed(() => appHotkeys.value['search.open'].hotkey)
</script>
<template>
<button @click="emit('search')" :aria-keyshortcuts="formatForAria(hotkey)">
Search <kbd v-if="hotkey">{{ formatForDisplay(hotkey) }}</kbd>
</button>
</template>Follow these rules when you add the attribute:
See Format for aria-keyshortcuts for the formatting rules and edge cases.
WCAG 2.1.4, Character Key Shortcuts (Level A) covers shortcuts made only of keys that type a character, such as K, ?, Shift+K, Space, or the sequence ['G', 'H']. Speech input types words as key presses, and people with tremors press keys by accident, so these shortcuts fire when nobody meant them to. An app with character key shortcuts must provide at least one of these options:
Add a setting, such as Single-key shortcuts, and suspend character key shortcuts while it is off. Pass suspend-character-keys to HotkeysProvider, set from the user's setting. While a provider with suspend-character-keys is mounted, document-level character key shortcuts across the whole app stop firing, so one provider at the root of the app is enough:
<script setup lang="ts">
import { HotkeysProvider } from '@tanstack/vue-hotkeys'
const settings = useSettings()
</script>
<template>
<HotkeysProvider :suspend-character-keys="!settings.characterKeyShortcuts">
<Editor />
</HotkeysProvider>
</template>With provideHotkeysContext, pass a getter or a ref: provideHotkeysContext({ suspendCharacterKeys: () => !settings.characterKeyShortcuts }).
Suspensions are counted. Character key shortcuts stay off while any mounted provider sets suspendCharacterKeys to true, and they resume when the last one unmounts or its prop becomes false. suspendCharacterKeys applies even when the provider sets :enabled="false". Outside a provider, call getHotkeyManager().suspendCharacterKeys(), which returns a function that resumes them.
While suspended, an enabled document-level character key shortcut reports the status 'suspended' (the same as a hotkey in an inactive scope), so a cheat sheet can mark it as off. Bind the setting's switch to the saved preference, not to the suspension. characterKeysSuspended in scope state is true while any suspension is in effect, so use it to show whether character key shortcuts work right now.
Suspension stops the callback but not preventDefault: 'always', which keeps blocking the key's browser action. Leave 'always' off character key shortcuts that users can turn off.
These hotkeys keep working while character keys are suspended:
| Hotkey | Why it keeps working |
|---|---|
| Mod+K, Control+K, Alt+K | Uses Control, Alt, or Meta. |
| Escape, Enter, Tab, ArrowUp, F1 | Does not type a character. |
| ['Mod+K', 'C'] | At least one step uses a modifier or a non-character key. |
| A hotkey with an element target, other than the body or the root element | Fires only while focus is inside that element. |
Suspension checks the effective hotkey, so an app hotkey remapped from K to Mod+K works even while character keys are suspended.
App hotkeys with a shortcut settings screen meet WCAG 2.1.4 on their own, as long as users can rebind every character key shortcut to one with Control, Alt, or Meta. Adding Shift alone still leaves a character key shortcut. Offering both a toggle and remapping lets users either disable single-key shortcuts or rebind individual ones.
A hotkey with an element target fires only while focus is inside that element. Use target for shortcuts that belong to one widget, such as J and K in a list. See Hotkey targets.
List every shortcut in a cheat sheet, and show modifier hints while a modifier is held. For physical-key bindings, use useFormatForDisplay() so labels match the user's keyboard layout (see Keyboard layouts).
Screen readers use single keys in browse mode and handle them before the page sees them. NVDA, for example, moves by heading with H and by link with K. Use Mod chords for app-wide actions, and reserve single-key shortcuts for focused widgets or an opt-in setting. Avoid binding browser or system shortcuts such as Mod+L or Mod+W.