Skip to content

Commit 57db93a

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 d2fb83a commit 57db93a

2 files changed

Lines changed: 18 additions & 4 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
@@ -61,6 +61,16 @@ describe("ghosttyConsumedMods", () => {
6161
expect(ghosttyConsumedMods({ ...shifted, ctrlKey: true })).toBe(0);
6262
expect(ghosttyConsumedMods({ ...shifted, key: "Tab" })).toBe(0);
6363
expect(ghosttyConsumedMods({ ...shifted, key: " " })).toBe(1);
64+
expect(ghosttyConsumedMods({ ...shifted, altKey: true }, "Linux x86_64")).toBe(0);
65+
});
66+
67+
it("consumes macOS Option when it produced a character", () => {
68+
const option = { ...shifted, altKey: true, key: "[", shiftKey: false };
69+
expect(ghosttyConsumedMods(option, "MacIntel")).toBe(1 << 2);
70+
expect(ghosttyConsumedMods({ ...option, shiftKey: true }, "MacIntel")).toBe(1 | (1 << 2));
71+
expect(ghosttyConsumedMods({ ...option, key: "ArrowLeft" }, "MacIntel")).toBe(0);
72+
expect(ghosttyConsumedMods({ ...option, ctrlKey: true }, "MacIntel")).toBe(0);
73+
expect(ghosttyConsumedMods({ ...option, metaKey: true }, "MacIntel")).toBe(0);
6474
});
6575
});
6676

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { US_SHIFTED_TO_UNSHIFTED_KEYS } from "@t3tools/shared/keybindings";
1+
import { isMacPlatform, US_SHIFTED_TO_UNSHIFTED_KEYS } from "@t3tools/shared/keybindings";
22

33
// This order mirrors GhosttyKey in ghostty/vt/key/event.h. The values are
44
// intentionally derived from the official W3C-aligned enum instead of
@@ -217,12 +217,16 @@ export function loadGhosttyKeyboardLayoutMap(): Promise<GhosttyKeyboardLayoutMap
217217
}
218218

219219
// Browsers do not expose consumed modifiers; treat Shift as consumed for
220-
// unchorded character input.
220+
// unchorded character input. On macOS, Option also composes layout characters
221+
// (Option+A is å), so it is consumed when it produced text, matching Ghostty's
222+
// `macos-option-as-alt = false`. The wasm encoder compiles that option out.
221223
export function ghosttyConsumedMods(
222224
event: Pick<KeyboardEvent, "altKey" | "ctrlKey" | "key" | "metaKey" | "shiftKey">,
225+
platform = navigator.platform,
223226
): number {
224-
if (!event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) return 0;
225-
return [...event.key].length === 1 ? 1 : 0;
227+
if (event.ctrlKey || event.metaKey || [...event.key].length !== 1) return 0;
228+
if (event.altKey && !isMacPlatform(platform)) return 0;
229+
return (event.shiftKey ? 1 : 0) | (event.altKey ? 1 << 2 : 0);
226230
}
227231

228232
export function ghosttyUnshiftedCodepoint(

0 commit comments

Comments
 (0)