Core Guides

Choosing Valid Hotkeys in Lit

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.

Why Shift+/ is a type error

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:

ts
@hotkey('Shift+/') // Type error
openHelp() {}

TypeScript 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:

  • You meant ?, the character that US Shift+/ types. The library reads it that way, so German Shift+ß fires it.
  • The string also says "Shift and /", and German Shift+7 is exactly that. So a German user who types a plain / fires it too.

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:

ts
import { LitElement, html } from 'lit'
import { customElement } from 'lit/decorators.js'
import { hotkey } from '@tanstack/lit-hotkeys'

@customElement('help-button')
class HelpButton extends LitElement {
  // Fires on whatever key types `?` on the user's layout
  @hotkey('?')
  openHelp() {
    // ...
  }

  render() {
    return html`<button @click=${() => this.openHelp()}>Help</button>`
  }
}

? 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.

Why letters and digits keep Shift

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.

What can follow Shift

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:

ts
@hotkey('Shift+/') // Type error
@hotkey('?') // OK
@hotkey('Control+?') // OK, fires on Control+Shift+/ on a US keyboard
@hotkey('Shift+[Slash]') // OK, the physical key
@hotkey('Mod+Shift+/') // Type error
@hotkey('Mod+?') // OK
openHelp() {}

The 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.

Mod with a symbol doesn't reach every Mac

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.

Replace a Shift string you already wrote

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:

AmbiguousUS QWERTYGermanFrench 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:

  • The table leaves out strings whose replacement is the same symbol on all three layouts, such as Shift+? and Shift+@. Write the symbol: ? or @.
  • With other modifiers, keep them and use the same replacement. On a US keyboard, Control+Shift+/ becomes Control+?, and Mod+Shift+/ becomes Mod+?.
  • Some replacements, such as French §, £, and °, aren't US symbols. To write them, register the layout's key set, as Keyboard layouts explains.
  • French µ isn't in FrenchKeys, so its cell names the key by position instead.
  • The German and French columns use the Windows layouts. The Mac layouts place some symbols differently.

To keep a key's position instead of its character, use Shift with the key's physical code, such as Shift+[Slash].

Physical codes name a key, not a character

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 codeUS QWERTYUS DvorakGerman
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 shortcuts belong to the browser or the operating system

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.

Browser tab shortcuts

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:

ChordActionChromeFirefoxSafari
Mod+WClose tabReservedReservedReported reserved
Mod+TNew tabReservedReservedReported reserved
Mod+NNew windowReservedReservedReported reserved
Mod+Shift+WClose windowReservedReserved
Mod+Shift+TReopen closed tabReserved
Mod+Shift+NNew private windowReserved
Control+Tab, Control+Shift+TabNext and previous tabReservedReported reservedReported reserved
Control+PageUp, Control+PageDownNext and previous tab, Windows and LinuxReserved
Mod+QQuit, macOSReservedNot reservedReported 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.

Operating system shortcuts

Each operating system keeps a few chords for itself. A chord is lost only on the system that keeps it:

  • Windows. Control+Alt+Delete, and most chords with the Windows key (Meta), such as Windows+L.
  • macOS. Command+Tab, Command+Space, and Command+Option+Escape.
  • Linux. Control+Alt+F1 through F12 switch to another virtual terminal. Desktops such as GNOME also take many chords with the Super key.

Users can change many of these, so no list is complete. Prefer Mod, Shift, and Alt chords over Meta chords, which Windows mostly keeps.

Chords that can't fire on some layouts

A few chords type a character the hotkey doesn't name, so they never fire on some layouts:

  • Option with a shifted symbol on a US Mac. Option+Shift+/ types ¿, not ?, so Alt+? and Mod+Alt+? never fire on a US Mac. Use the physical code instead, such as Alt+Shift+[Slash].
  • Control+Alt on layouts with AltGr. Windows treats Control+Alt as AltGr. On a German keyboard, Control+Alt+Q types @, so Control+Alt+Q never fires, and an @ binding does.

Let users choose their own shortcuts

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:

  • It records only presses that reach the page, so in a browser tab it never records the browser and operating system chords above.
  • It never writes Shift with a symbol. By default, Shift+/ records ?, and German Shift+7 records /. With recordBy: 'code', it records the physical key instead, such as Shift+[Slash].
  • On a Mac with Command held, off the key's US position, the browser may not report the symbol the user typed. There the recorder records the key's physical code: Dvorak Command+Shift+/ records Mod+Shift+[BracketLeft]. Where macOS reports a symbol only when Shift typed it, such as ?, the recorder keeps the symbol: French Command+Shift on the ? key records Mod+?.

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:

ts
import { LitElement, html } from 'lit'
import { customElement } from 'lit/decorators.js'
import { HotkeyRecorderController } from '@tanstack/lit-hotkeys'

const browserTabShortcuts = new Set([
  'Mod+W',
  'Mod+T',
  'Mod+N',
  'Mod+Shift+N',
  'Mod+Shift+T',
  'Mod+Shift+W',
])

@customElement('shortcut-input')
class ShortcutInput extends LitElement {
  private recorder = new HotkeyRecorderController(this, {
    validate: (hotkey) =>
      !browserTabShortcuts.has(hotkey) || 'The browser uses this shortcut.',
    onRecord: (hotkey) =>
      this.dispatchEvent(new CustomEvent('save', { detail: hotkey })),
  })

  render() {
    return html`
      <button @click=${() => this.recorder.startRecording()}>
        Record a shortcut
      </button>
    `
  }
}