You write a hotkey on your keyboard, and your users press it on theirs. Their keyboard can have another layout, another operating system, and another browser, and each of those can change what a key press means. A shortcut that works on your machine can fire on the wrong key for a German user, or never fire for someone on a Mac.
TanStack Hotkeys absorbs most of these differences at runtime. A few it can't, because the hotkey string itself means more than one thing. For those, the types reject the string while you write it, so you find out in your editor and not from a bug report. This guide explains why those strings are rejected and what to write instead. It also covers the shortcuts no string can fix, because the browser or the operating system handles them before your app does.
Say you want ? to open a help dialog. On a US keyboard you type ? with Shift and the / key, so you write down what you press:
createHotkey('Shift+/', openHelp) // Type errorTypeScript rejects it. That can feel strict, since the string looks reasonable and would work on your keyboard. The problem shows up on other keyboards.
A hotkey string names characters, and layouts put / and ? on different keys. On a German keyboard, / is Shift+7 and ? is Shift+ß. There, Shift+/ can be read two ways:
If your app binds / to search as well, that German user gets your help dialog instead of search, because an exact Shift match ranks above /. The library can't tell which reading you meant. Only you know that, so the types ask you to say it.
When you mean the character, write the character:
<script lang="ts">
import { createHotkey } from '@tanstack/svelte-hotkeys'
// Fires on whatever key types `?` on the user's layout
createHotkey('?', () => openHelp())
</script>? means the same thing on every layout. It fires on Shift+/ on a US keyboard and on Shift+ß on a German one, and the matcher takes care of the Shift the user needs to type it.
When you mean the key's position, name the physical key. Shift+[Slash] is Shift with the key that US keyboards label /, whatever that key types on the user's layout.
Shift changes a letter's case, not which letter it is. Shift+K names the same key on every layout, so the types allow it. The Shift matters there too: K and Shift+K are two different shortcuts.
Digits are safe for another reason. The matcher reads the digit row by position, so Shift+1 fires on the key that types 1 on every layout. That includes French AZERTY, where the key types & without Shift.
A symbol gets neither guarantee. Which symbol Shift adds to a key is up to the layout.
After Shift, with or without other modifiers, a hotkey string takes a letter, a digit, a named key such as Tab, or a physical code such as [Slash]. A symbol after Shift is a type error:
createHotkey('Shift+/', openHelp) // Type error
createHotkey('?', openHelp) // OK
createHotkey('Control+?', openHelp) // OK, fires on Control+Shift+/ on a US keyboard
createHotkey('Shift+[Slash]', openHelp) // OK, the physical key
createHotkey('Mod+Shift+/', openHelp) // Type error
createHotkey('Mod+?', openHelp) // OKThe same rule covers letters from registered keyboard layouts: Shift+Ä is allowed, and Shift+§ is a type error.
The rule lives only in the types. A string from storage or user settings, such as a saved 'Shift+/', still matches as it did before, so shortcuts your users already saved keep working.
In .svelte files, svelte-check reports these errors. tsc alone doesn't read them.
Mod+? is how you write Control+Shift+/ on Windows and Command+Shift+/ on macOS. On Windows and Linux it fires on every layout. macOS is less predictable.
With Command held, macOS reports a different character for some keys than the one they type. Command+Shift on the key that types ? reports / on German and Dvorak, ' on Spanish, and + on Swedish. So Mod+? doesn't fire on those Mac layouts, while it does on US, UK, French, and Italian Macs.
No symbol spelling covers that press on every Mac layout. When a shortcut has to work for everyone, such as the one that opens your command palette, give it a letter or a digit, such as Mod+Shift+K or Mod+Shift+1. To bind a key by position, use its physical code, such as Mod+Shift+[Slash].
Keys match the character the user types has the details of how Command changes the reported key.
When existing code has Shift+ with a symbol, the right replacement depends on the keyboard it was written on, because whoever wrote it wrote what their keyboard typed. Shift+, types < on a US keyboard, ; on a German one, and ? on a French one. Find the string in the first column, then take the replacement from the column for the layout it was written on:
| Ambiguous | US QWERTY | German | French AZERTY |
|---|---|---|---|
| Shift+` | ~ | ` | ` |
| Shift+- | _ | _ | Shift+6 |
| Shift+= | + | = | + |
| Shift+[ | { | [ | [ |
| Shift+] | } | ] | ] |
| Shift+\ | | | \ | \ |
| Shift+; | : | ; | . |
| Shift+' | " | ' | Shift+4 |
| Shift+, | < | ; | ? |
| Shift+. | > | : | . |
| Shift+/ | ? | / | / |
| Shift+_ | _ | _ | Shift+8 |
| Shift++ | + | * | + |
| Shift+: | : | : | / |
| Shift+" | " | " | Shift+3 |
| Shift+< | < | > | > |
| Shift+! | ! | ! | § |
| Shift+# | # | ' | # |
| Shift+$ | $ | $ | £ |
| Shift+& | & | & | Shift+1 |
| Shift+* | * | * | Shift+[Backslash] |
| Shift+( | ( | ( | Shift+5 |
| Shift+) | ) | ) | ° |
Each cell is what Shift and that key type on that layout. When the layout has no key that types the symbol without Shift, the replacement is the symbol itself. German / is Shift+7, so a German developer who wrote Shift+/ meant /. Where Shift types a digit, as on the French digit row, the cell keeps Shift. A digit names its key by position, so 1 alone would also fire on the & that key types without Shift.
A few more rules for reading the table:
To keep a key's position instead of its character, use Shift with the key's physical code, such as Shift+[Slash].
Use a physical code when the key's position matters more than its label, such as keys arranged in a row for a game or a drawing tool. The catch is the label. On other layouts the key types something else, so a hint that shows the US symbol can be wrong for those users. Shift and these codes type:
| Physical code | US QWERTY | US Dvorak | German |
|---|---|---|---|
| Shift+[Backquote] | ~ | ~ | ° |
| Shift+[Minus] | _ | { | ? |
| Shift+[Equal] | + | } | a dead key |
| Shift+[BracketLeft] | { | ? | Ü |
| Shift+[BracketRight] | } | + | * |
| Shift+[Backslash] | | | | | ' |
| Shift+[Semicolon] | : | S | Ö |
| Shift+[Quote] | " | _ | Ä |
| Shift+[Comma] | < | W | ; |
| Shift+[Period] | > | V | : |
| Shift+[Slash] | ? | Z | _ |
When you pass a layout map, formatForDisplay labels a physical code with the key's character on the user's layout. On a German keyboard, Shift+[Slash] shows as Shift+-. Without a layout map, which only Chromium browsers provide, the label is the US character for that position.
Some chords never reach your page. The browser or the operating system handles them first, so your handler doesn't run, and the user gets the browser's action instead.
The types still accept these chords, and that's deliberate. The same chord can be free in an installed web app, in fullscreen, or in Electron. The types can't know where your app runs, but you do.
In a normal browser tab, these chords open, close, or switch tabs and windows. Chrome reserves them. The Firefox and Safari columns come from their source and bug reports, so treat a blank cell as unconfirmed, not as free:
| Chord | Action | Chrome | Firefox | Safari |
|---|---|---|---|---|
| Mod+W | Close tab | Reserved | Reserved | Reported reserved |
| Mod+T | New tab | Reserved | Reserved | Reported reserved |
| Mod+N | New window | Reserved | Reserved | Reported reserved |
| Mod+Shift+W | Close window | Reserved | Reserved | |
| Mod+Shift+T | Reopen closed tab | Reserved | ||
| Mod+Shift+N | New private window | Reserved | ||
| Control+Tab, Control+Shift+Tab | Next and previous tab | Reserved | Reported reserved | Reported reserved |
| Control+PageUp, Control+PageDown | Next and previous tab, Windows and Linux | Reserved | ||
| Mod+Q | Quit, macOS | Reserved | Not reserved | Reported reserved |
Chrome reserves none of these in an installed web app window, and in fullscreen it keeps only the keys that leave fullscreen. If your app runs in a browser tab, pick other chords for its own actions.
Each operating system keeps a few chords for itself. A chord is lost only on the system that keeps it:
Users can change many of these, so no list is complete. Prefer Mod, Shift, and Alt chords over Meta chords, which Windows mostly keeps.
A few chords type a character the hotkey doesn't name, so they never fire on some layouts:
The most reliable shortcut is often the one a user picks on their own keyboard. The hotkey recorder follows the rules in this guide for you:
A recorded hotkey can still fail on another machine. Alt+F4 recorded on a Mac closes the window on Windows. See Save hotkeys that work on another platform to keep synced hotkeys portable.
To reject chords you don't want, such as the browser tab shortcuts in an Electron app that also ships to the web, return an error from validate:
<script lang="ts">
import { createHotkeyRecorder } from '@tanstack/svelte-hotkeys'
let { onSave }: { onSave: (hotkey: string) => void } = $props()
const browserTabShortcuts = new Set([
'Mod+W',
'Mod+T',
'Mod+N',
'Mod+Shift+N',
'Mod+Shift+T',
'Mod+Shift+W',
])
const recorder = createHotkeyRecorder({
validate: (hotkey) =>
!browserTabShortcuts.has(hotkey) || 'The browser uses this shortcut.',
onRecord: (hotkey) => onSave(hotkey),
})
</script>
<button onclick={recorder.startRecording}>Record a shortcut</button>