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 injectFormatForAria() to format a hotkey for the attribute using the same platform and keyboard layout as the display formatter:
import { Component } from '@angular/core'
import {
injectAppHotkeys,
injectFormatForAria,
injectFormatForDisplay,
injectHotkey,
} from '@tanstack/angular-hotkeys'
@Component({
selector: 'app-search-button',
template: `
@let hotkey = appHotkeys()['search.open'].hotkey;
<button
(click)="openSearch()"
[attr.aria-keyshortcuts]="formatForAria(hotkey)"
>
Search
@if (hotkey) {
<kbd>{{ formatForDisplay(hotkey) }}</kbd>
}
</button>
`,
})
export class SearchButton {
appHotkeys = injectAppHotkeys()
formatForAria = injectFormatForAria()
formatForDisplay = injectFormatForDisplay()
constructor() {
injectHotkey(
() => this.appHotkeys()['search.open'],
() => this.openSearch(),
)
}
openSearch() {
// ...
}
}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 suspendCharacterKeys to provideHotkeys as a getter that reads the user's setting. While that injector lives, document-level character key shortcuts across the whole app stop firing, so providing it in the application config is enough:
import { inject } from '@angular/core'
import type { ApplicationConfig } from '@angular/core'
import { provideHotkeys } from '@tanstack/angular-hotkeys'
export const appConfig: ApplicationConfig = {
providers: [
provideHotkeys({
// Runs in the provider's injection context, so it can read a service.
suspendCharacterKeys: () => !inject(Settings).characterKeyShortcuts(),
}),
],
}Angular creates a provider when something under it first injects it, and the setting applies from then on. Put suspendCharacterKeys on the application's provideHotkeys. Hotkeys registered with injectHotkey and the other inject functions inject it first, so they're always covered. A hotkey registered directly with getHotkeyManager().register() isn't suspended until a component first injects the provider.
Suspensions are counted. Character key shortcuts stay off while any active provider evaluates suspendCharacterKeys to true, and they resume when the last one is destroyed or its value 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 injectFormatForDisplay() 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.