Skip to content

Commit d48a064

Browse files
committed
fix(web): terminal types macOS Option-layer characters
On many macOS layouts Option is how you type basic characters (German Option+L for @, Finnish Option+7 for |, Neo2 layer 3 brackets). The Ghostty terminal forwarded Option as a live Alt modifier, so the encoder sent ESC plus the key instead of the character the browser already put in event.key. On macOS, report Option (and Shift) as consumed when the chord produced a single character, matching Ghostty's macos-option-as-alt = false. The wasm build compiles that encoder option out, so consumed mods are the only lever. Ctrl/Cmd chords and non-text keys keep their encodings, and nothing changes off macOS. Fixes #13046
1 parent 1451e7c commit d48a064

2 files changed

Lines changed: 19 additions & 3 deletions

File tree

‎apps/web/src/terminal/ghostty/keyCodes.test.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,16 @@ describe("ghosttyConsumedMods", () => {
4747
expect(ghosttyConsumedMods({ ...shifted, ctrlKey: true })).toBe(0);
4848
expect(ghosttyConsumedMods({ ...shifted, key: "Tab" })).toBe(0);
4949
expect(ghosttyConsumedMods({ ...shifted, key: " " })).toBe(1);
50+
expect(ghosttyConsumedMods({ ...shifted, altKey: true }, "Linux x86_64")).toBe(0);
51+
});
52+
53+
it("consumes macOS Option when it produced a character", () => {
54+
const option = { ...shifted, altKey: true, key: "[", shiftKey: false };
55+
expect(ghosttyConsumedMods(option, "MacIntel")).toBe(1 << 2);
56+
expect(ghosttyConsumedMods({ ...option, shiftKey: true }, "MacIntel")).toBe(1 | (1 << 2));
57+
expect(ghosttyConsumedMods({ ...option, key: "ArrowLeft" }, "MacIntel")).toBe(0);
58+
expect(ghosttyConsumedMods({ ...option, ctrlKey: true }, "MacIntel")).toBe(0);
59+
expect(ghosttyConsumedMods({ ...option, metaKey: true }, "MacIntel")).toBe(0);
5060
});
5161
});
5262

‎apps/web/src/terminal/ghostty/keyCodes.ts‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { isMacPlatform } from "@t3tools/shared/keybindings";
2+
13
// This order mirrors GhosttyKey in ghostty/vt/key/event.h. The values are
24
// intentionally derived from the official W3C-aligned enum instead of
35
// maintaining a second keyboard protocol.
@@ -239,12 +241,16 @@ export function loadGhosttyKeyboardLayoutMap(): Promise<GhosttyKeyboardLayoutMap
239241
}
240242

241243
// Browsers do not expose consumed modifiers; treat Shift as consumed for
242-
// unchorded character input.
244+
// unchorded character input. On macOS, Option also composes layout characters
245+
// (Option+A is å), so it is consumed when it produced text, matching Ghostty's
246+
// `macos-option-as-alt = false`. The wasm encoder compiles that option out.
243247
export function ghosttyConsumedMods(
244248
event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "key" | "metaKey" | "shiftKey">,
249+
platform = navigator.platform,
245250
): number {
246-
if (!event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) return 0;
247-
return [...event.key].length === 1 ? 1 : 0;
251+
if (event.ctrlKey || event.metaKey || [...event.key].length !== 1) return 0;
252+
if (event.altKey && !isMacPlatform(platform)) return 0;
253+
return (event.shiftKey ? 1 : 0) | (event.altKey ? 1 << 2 : 0);
248254
}
249255

250256
export function ghosttyUnshiftedCodepoint(

0 commit comments

Comments
 (0)