Skip to content
Blog

Shortcuts that survive any layout: match the physical key

AtomCut matches KeyboardEvent.code, not the typed character — so ⌘Z fires under Persian, Arabic or Cyrillic layouts, and ⌥M isn't swallowed as 'µ'.

SShayanBuilding AtomCut
One single blank keycap in dark metal on a graphite slab, crossed by a line of orange and pink light.

If you build keyboard shortcuts against KeyboardEvent.key and test only on a US layout, your app has a bug you can't see yet. Under a Persian, Arabic, Cyrillic or Greek keyboard, ⌘Z does nothing, because the key event that fires when your user presses the key in the position labeled Z reports whatever character that layout types there — which is not "z". On a Mac's own US layout, ⌥M types "µ" before your handler ever sees a "m". AtomCut shipped this bug for a while, in 2.17.0 fixed it, and the fix is one function that around thirty call sites across the app now go through instead of reading e.key themselves.

The bug, plainly

KeyboardEvent.key reports the character a layout produces for a given physical key, given whatever modifiers are held. That's exactly the right thing to read for "what did the user type" — a text field wants the letter their layout actually produces. It's exactly the wrong thing to read for "which command did the user invoke," because a shortcut like ⌘Z is a statement about a specific key's position, the one your muscle memory reaches for, not about what character that position happens to produce this week on this OS under this layout.

Two failure modes come out of that one confusion:

  • A shortcut that should fire, doesn't. Under a Persian layout, the physical key in the QWERTY-Z position types a Persian character, not "z". A handler checking e.key === "z" never matches, so ⌘Z is silently dead for that user.
  • A shortcut that should fire, gets eaten first. On a Mac, ⌥ is a dead-key modifier for a whole set of letters even under the US layout — ⌥M produces "µ". If your handler is waiting for e.key === "m" with altKey: true, it never sees "m"; it sees "µ", and the shortcut silently doesn't exist.

Neither of these shows up if you only ever test on your own machine with your own layout. They show up in a bug report from someone in Tehran, or from someone who just held Option a beat too long.

The fix: trust the layout only when it names something usable

KeyboardEvent.code is the other half of the picture: it names the physical key, using the US layout's name for it, regardless of what layout is actually active. KeyCode.KeyZ is always the key in the Z position, whether the OS is typing "z", a Persian character, or a Cyrillic one there. The fix isn't "always use code instead of key" — that would break the cases where the layout-typed character genuinely is the right thing to display and record, like AZERTY's A or Dvorak's O, which people expect their own shortcuts to be built around. The fix is a rule for which one to trust, per event:

ts
// apps/web/src/editor/state/shortcut-registry.ts
/**
 * Does the layout name this key with something a combo can be written in — a
 * printable ASCII character, or a named key (`escape`, `arrowleft`, `f5`)?
 * `dead`, `process` and `unidentified` are the platform saying it does NOT
 * know: a ⌥-dead-key on a Mac, an IME mid-composition, a keyless soft key.
 */
function isLayoutNamed(key: string): boolean {
  if (key.length === 1) {
    const c = key.charCodeAt(0);
    return c >= 0x20 && c <= 0x7e;
  }
  return key.length > 1 && key !== "dead" && key !== "process" && key !== "unidentified";
}

If the layout gives a plain ASCII character or a recognizable named key, that's the keycap the user is looking at, and it's honored as written. If it gives something else, a non-Latin character, or the literal string "dead" a Mac reports mid-composition for a dead key like ⌥, the code falls back to the physical key's US name:

ts
// apps/web/src/editor/state/shortcut-registry.ts
export function eventKey(e: KeyboardEvent | React.KeyboardEvent): string {
  const key = typeof e.key === "string" ? e.key.toLowerCase() : "";
  if (isLayoutNamed(key)) return key;
  const physical = typeof e.code === "string" ? CODE_KEYS[e.code] : undefined;
  return physical ?? key;
}

That's the whole rule. Every shortcut handler, every hand-rolled meta && key === "z" that used to exist scattered through the app, and the settings panel's own shortcut recorder all call this one function now, which is what guarantees a combo recorded under one layout matches under every other — the recorder and the dispatcher can't disagree, because they're the same code.

The map is deliberately small

CODE_KEYS only needs to cover the keys whose e.key name actually changes across layouts: letters, digits, and the punctuation row. Named keys like Escape, ArrowLeft or F5 report the identical string in every layout already, so they never need a fallback:

ts
// apps/web/src/editor/state/shortcut-registry.ts
const CODE_KEYS: Record<string, string> = {
  Minus: "-",
  Equal: "=",
  BracketLeft: "[",
  BracketRight: "]",
  Backslash: "\\",
  Semicolon: ";",
  Quote: "'",
  Backquote: "`",
  Comma: ",",
  Period: ".",
  Slash: "/",
  Space: " ",
};
for (let i = 0; i < 26; i++) {
  const letter = String.fromCharCode(97 + i);
  CODE_KEYS[`Key${letter.toUpperCase()}`] = letter;
}
for (let i = 0; i < 10; i++) {
  CODE_KEYS[`Digit${i}`] = String(i);
}

This is the same instinct as the transform rules I've written about elsewhere: fix the actual scope of the problem, not everything adjacent to it. Building a full code-to-key table for every named key on the board would be more code doing nothing, since those keys were never broken.

A quieter bug this also killed

There's a second payoff that has nothing to do with layouts. KeyboardEvent.key isn't guaranteed to be a normal string — soft keyboards, IMEs, and dictation on iPadOS can dispatch key events with no usable key at all. Every bare e.key.toLowerCase() scattered through a codebase is a crash waiting on a tablet, reachable from however many unrelated call sites wrote that pattern. Because eventKey is the one reader, it made the "no key at all" case a non-match instead of a thrown exception, everywhere at once:

ts
// apps/web/src/editor/state/shortcut-registry.ts
/**
 * Never throwing. `KeyboardEvent.key` IS NOT GUARANTEED. Soft keyboards,
 * IMEs and dictation on iPadOS dispatch key events carrying no `key` at all,
 * and every bare `e.key.toLowerCase()` in the editor threw on them — a crash
 * reachable only on a tablet, from thirty unrelated places. One reader, so a
 * keyless event is a shortcut that does not match rather than a dead editor.
 */

That's the actual argument for centralizing something like this even before you know about the layout bug: thirty places reading a value the same slightly-wrong way is thirty places that have to be found and fixed individually the day the assumption breaks. One place is one fix.

It showed up under a Persian layout first, then the same shape under Arabic, Cyrillic and Greek: ⌘Z did nothing, and ⌥M arrived as µ.

The whole registry, including eventKey and comboFromEvent, is in apps/web/src/editor/state/shortcut-registry.ts. See it in the repo.