Format hotkeys for display whenever they appear in buttons, menus, tooltips, cheat sheets, or settings screens. Store the raw hotkey string (such as 'Mod+S') and format it at render time. Labels are for humans, never for registering or storing.
useFormatForDisplay returns a formatter that adapts to the user's operating system (⌘ S on macOS, Ctrl+S elsewhere) and keyboard layout:
| Where labels render | Extra setup |
|---|---|
| Browser-only (Vite SPA, client apps) | None |
| Server-rendered then hydrated | Set platform on HotkeysProvider from the request (SSR & Hydration) |
| Static or prerendered pages | None, or wrap in a client-only component (Static pages) |
Call useFormatForDisplay() at the top of your component, then call the returned function wherever you render shortcuts:
import { useFormatForDisplay } from '@tanstack/react-hotkeys'
function SaveButton() {
const formatForDisplay = useFormatForDisplay()
return (
<button>
Save <kbd>{formatForDisplay('Mod+S')}</kbd>
</button>
)
}The hook wires up two things automatically:
Call the returned formatForDisplay function anywhere in the component, including inside .map() loops and conditionals. Pass a second argument to override the platform or formatting for one call:
formatForDisplay('Mod+S', { platform: 'mac' }) // '⌘ S'
formatForDisplay('Mod+[KeyS]', { platform: 'mac' }) // '⌘ S'
formatForDisplay({ code: 'KeyS', mod: true }, { platform: 'windows' }) // 'Ctrl+S'
formatForDisplay('Mod+[Digit2]', { platform: 'windows' }) // 'Ctrl+2'How chords format:
Pass parts: true to receive an array of key strings instead of a joined label, which works well for styling individual <kbd> tags:
const saveHotkey = 'Mod+[KeyS]'
const parts = formatForDisplay(saveHotkey, { platform: 'mac', parts: true })
// ['⌘', 'S']parts: true keeps literal + keys intact and ignores separatorToken. Note that passing a dynamic boolean for parts widens the return type to string | string[].
Pass a sequence array to format every step, separated by sequenceSeparator (a space by default):
formatForDisplay(['Mod+[KeyK]', 'C'], { platform: 'mac' }) // '⌘ K C'
formatForDisplay(['G', 'G'], { sequenceSeparator: ' → ' }) // 'G → G'
formatForDisplay(['Mod+[KeyK]', 'C'], { platform: 'mac', parts: true })
// [['⌘', 'K'], ['C']], one array of keycaps per stepTo display in-progress steps as the user types a sequence, see Show sequences in your UI.
Customize modifier and key symbols (useSymbols) and separators (separatorToken, sequenceSeparator):
formatForDisplay('Shift+[ArrowUp]', {
platform: 'mac',
useSymbols: { modifiers: false, keys: true },
parts: true,
}) // ['Shift', '↑']
formatForDisplay('Mod+[KeyS]', {
platform: 'mac',
useSymbols: false,
}) // 'Cmd+S'
formatForDisplay('Control++', {
platform: 'windows',
separatorToken: ' · ',
}) // 'Ctrl · +'useSymbols accepts a boolean or { modifiers?, keys? }. Omitted fields default to true, and modifiers affects macOS only because Windows and Linux always use text modifier names. Pass '' to join parts directly, or undefined or null for platform defaults.
Physical bindings follow key position ([KeyW] is always the US W slot), which sits at Z on a French AZERTY keyboard. useFormatForDisplay() automatically resolves physical codes to the character printed on the user's layout where supported:
import { useFormatForDisplay } from '@tanstack/react-hotkeys'
function MoveUp() {
const formatForDisplay = useFormatForDisplay()
return <kbd>{formatForDisplay('[KeyW]')}</kbd> // 'W' on QWERTY, 'Z' on AZERTY
}Important
Browser support. Keyboard layout detection (navigator.keyboard.getLayoutMap()) works only in Chromium browsers (Chrome and Edge). Firefox and Safari don't expose layout maps and fall back to US QWERTY labels (W or Q). Inside cross-origin iframes, Chromium also requires allow="keyboard-map".
Good to know:
To draw your own layout-aware UI, such as an on-screen keyboard, call useKeyboardLayout(), which returns the loaded map (code to character) or null. To turn off browser layout detection altogether, pass keyboardLayout: false on HotkeysProvider:
<HotkeysProvider defaultOptions={{ keyboardLayout: false }}>
<App />
</HotkeysProvider>If your app already tracks a user's layout preference, pass any Map-like { get(code): string | undefined } object as layoutMap, which overrides the browser map:
const layoutMap = new Map([['KeyQ', 'a']])
formatForDisplay('Mod+[KeyQ]', { platform: 'mac', layoutMap }) // '⌘ A'
formatForDisplay('Mod+Q', { platform: 'mac', layoutMap }) // '⌘ Q' (logical key unchanged)Use keyLabels to customize display names by physical code or logical key name. It works on the server too, and overrides both layoutMap and built-in fallbacks:
formatForDisplay('Mod+[KeyQ]', {
platform: 'mac',
layoutMap,
keyLabels: { KeyQ: 'Action' },
}) // '⌘ Action'To render platform-accurate labels on the server without hydration mismatches or a Linux-label flash on macOS and Windows, pass platform to HotkeysProvider from the incoming request, or wrap labels in a client-only guard on static pages. See SSR & Hydration for TanStack Start, Next.js, React Router, Astro, and static page examples.
Hooks can't run inside React Server Components, plain modules, or unit tests. Call formatForDisplay directly and pass platform yourself:
import { formatForDisplay } from '@tanstack/react-hotkeys'
formatForDisplay('Mod+S', { platform }) // '⌘ S' when platform is 'mac'formatForDisplay doesn't read HotkeysProvider and uses only the layoutMap you pass. To feed it the browser layout outside React, call loadKeyboardLayout() and read getKeyboardLayoutStore().
Note
formatForDisplay is marked @deprecated on the React package so editor autocomplete nudges component code toward useFormatForDisplay(). Outside components, calling it directly is expected.
Use the core helpers for persistence, comparison, and validation:
import { normalizeHotkey, parseHotkey } from '@tanstack/react-hotkeys'
const parsed = parseHotkey('Mod+[KeyS]', 'mac')
if (parsed.code !== undefined) {
console.log(parsed.code) // 'KeyS' (parsed.key is undefined)
}
const stored = normalizeHotkey(parsed, 'mac') // 'Mod+[KeyS]'validateHotkey checks syntax only, so map reason codes to your own UI strings. To validate as users press keys, see Validate and check conflicts.
Try these formatting options interactively in the TanStack Start example, or see Cheat Sheets for rendering full shortcut lists.