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.
createFormatForDisplay returns a formatter that adapts to the user's operating system (⌘ S on macOS, Ctrl+S elsewhere) and keyboard layout. It formats chord strings, including physical ones such as 'Mod+[KeyS]', chord objects, parsed chords, and sequences, and works wherever labels render:
| 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 | Wrap labels in a client-only component (Static pages) |
Call createFormatForDisplay in a component, then call the returned function wherever shortcuts render:
import { createFormatForDisplay } from '@tanstack/solid-hotkeys'
function SaveButton() {
const formatForDisplay = createFormatForDisplay()
return (
<button>
Save <kbd>{formatForDisplay('Mod+S')}</kbd>
</button>
)
}The label is ⌘ S on macOS and Ctrl+S on Windows and Linux. The primitive wires up two things automatically:
Call the returned function anywhere in the component, including inside <For> loops and conditionals. Pass a second argument to override the platform or formatting for one call. The examples in this guide pass platform, so each one has a single result:
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. createFormatForDisplay automatically resolves physical codes to the character printed on the user's layout where supported:
import { createFormatForDisplay } from '@tanstack/solid-hotkeys'
function MoveUp() {
const formatForDisplay = createFormatForDisplay()
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 createKeyboardLayout(), which returns an accessor for 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'keyLabels wins over a layout entry, which wins over the fallback label. Layout entries get normal letter casing and key symbols, and missing or empty entries fall back. All of these options affect display only.
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 SolidStart, TanStack Start, Astro, and static page examples.
createFormatForDisplay is a primitive, so it can't run in a test or a plain module. Call formatForDisplay directly and pass platform yourself:
import { formatForDisplay } from '@tanstack/solid-hotkeys'
formatForDisplay('Mod+S', { platform }) // '⌘ S' when platform is 'mac'formatForDisplay doesn't read HotkeysProvider and uses only the layoutMap you pass. Without a platform, it detects one in the browser and uses 'linux' on a server. To feed it the browser layout outside Solid, call loadKeyboardLayout() and read getKeyboardLayoutStore().
Note
formatForDisplay is marked @deprecated on the Solid package so editor autocomplete nudges component code toward createFormatForDisplay(). Outside components, calling it directly is expected.
Use the core helpers for persistence, comparison, and validation:
import { normalizeHotkey, parseHotkey } from '@tanstack/solid-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.