The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 22, 22:00 UTC
fix(web): suppress terminal key releases for app-handled shortcuts (#15020)
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> Co-authored-by: SAPHID <saphid@users.noreply.github.com> Co-authored-by: Julius Marminge <julius0216@outlook.com>
Alex Southwell committed
Oct 20, 2026 at 05:06 UTC
e433eeb7a35d745cfc144fc30ac66e2f6d528381
4 files changed
+191
-70
apps/web/src/components/ThreadTerminalDrawer.permissions.browser.test.tsx
+102
-1
@@ -82,7 +82,7 @@ vi.mock(import("../state/session"), async (original) => {
82
};
83
});
84
85
-import { TerminalViewport } from "./ThreadTerminalDrawer";
85
+import ThreadTerminalDrawer, { TerminalViewport } from "./ThreadTerminalDrawer";
86
87
const threadRef = {
88
environmentId: EnvironmentId.make("secondary-terminal"),
@@ -110,6 +110,7 @@ afterEach(async () => {
110
await act(async () => root.unmount());
111
container.remove();
112
vi.unstubAllGlobals();
113
+ vi.restoreAllMocks();
114
});
115
116
it("rechecks the target terminal grant when a retained surface callback reports a resize", async () => {
@@ -154,3 +155,103 @@ it("rechecks the target terminal grant when a retained surface callback reports
155
input: { threadId: threadRef.threadId, terminalId: "terminal-1", cols: 100, rows: 30 },
156
});
157
});
158
+
159
+it("keeps held Kitty keys and the original surface through split, focus, and unsplit", async () => {
160
+ const { GhosttyTerminalSurface } = await vi.importActual<
161
+ typeof import("~/terminal/ghostty/surface")
162
+ >("~/terminal/ghostty/surface");
163
+ const output: string[][] = [];
164
+ state.createSurface.mockImplementation((mount, options) => {
165
+ const data: string[] = [];
166
+ output.push(data);
167
+ return GhosttyTerminalSurface.create(mount, {
168
+ ...options,
169
+ onData: (chunk) => {
170
+ data.push(chunk);
171
+ options.onData(chunk);
172
+ },
173
+ });
174
+ });
175
+ const consumeShortcut = (event: KeyboardEvent) => {
176
+ if ((event.code === "KeyD" && event.ctrlKey) || (event.code === "KeyB" && event.repeat)) {
177
+ event.preventDefault();
178
+ event.stopPropagation();
179
+ }
180
+ };
181
+ // ChatView installs its capture listener before the terminal mounts.
182
+ window.addEventListener("keydown", consumeShortcut, { capture: true });
183
+ const composer = document.createElement("textarea");
184
+ document.body.append(composer);
185
+ const render = async (terminalIds: string[]) => {
186
+ await act(async () =>
187
+ root.render(
188
+ <ThreadTerminalDrawer
189
+ threadRef={threadRef}
190
+ threadId={threadRef.threadId}
191
+ cwd="/repo"
192
+ height={280}
193
+ terminalIds={terminalIds}
194
+ activeTerminalId="original"
195
+ terminalGroups={[{ id: "group", terminalIds }]}
196
+ activeTerminalGroupId="group"
197
+ focusRequestId={0}
198
+ onSplitTerminal={state.otherCommand}
199
+ onSplitTerminalVertical={state.otherCommand}
200
+ onNewTerminal={state.otherCommand}
201
+ onActiveTerminalChange={state.otherCommand}
202
+ onCloseTerminal={state.otherCommand}
203
+ onHeightChange={state.otherCommand}
204
+ onAddTerminalContext={state.otherCommand}
205
+ keybindings={[]}
206
+ />,
207
+ ),
208
+ );
209
+ await act(async () => {
210
+ await Promise.all(state.createSurface.mock.results.map((result) => result.value));
211
+ });
212
+ };
213
+ try {
214
+ await render(["original"]);
215
+ const original = await state.createSurface.mock.results[0]!.value;
216
+ const disposeOriginal = vi.spyOn(original, "dispose");
217
+ original.write("\x1b[>11u");
218
+ const key = (type: "keydown" | "keyup", init: KeyboardEventInit) =>
219
+ original.input.dispatchEvent(
220
+ new KeyboardEvent(type, { bubbles: true, cancelable: true, ...init }),
221
+ );
222
+ original.focus();
223
+ key("keydown", { key: "Control", code: "ControlLeft", ctrlKey: true });
224
+ key("keydown", { key: "d", code: "KeyD", ctrlKey: true });
225
+ await render(["original", "split"]);
226
+ expect(state.createSurface).toHaveBeenCalledTimes(2);
227
+ expect(container.querySelector("textarea")).toBe(original.input);
228
+ expect(disposeOriginal).not.toHaveBeenCalled();
229
+ const split = await state.createSurface.mock.results[1]!.value;
230
+ const disposeSplit = vi.spyOn(split, "dispose");
231
+ original.focus();
232
+ key("keyup", { key: "d", code: "KeyD", ctrlKey: true });
233
+ key("keyup", { key: "Control", code: "ControlLeft" });
234
+
235
+ key("keydown", { key: "b", code: "KeyB" });
236
+ composer.focus();
237
+ original.focus();
238
+ key("keydown", { key: "b", code: "KeyB", repeat: true });
239
+ key("keyup", { key: "b", code: "KeyB" });
240
+
241
+ key("keydown", { key: "c", code: "KeyC" });
242
+ await render(["original"]);
243
+ expect(state.createSurface).toHaveBeenCalledTimes(2);
244
+ expect(container.querySelector("textarea")).toBe(original.input);
245
+ expect(disposeOriginal).not.toHaveBeenCalled();
246
+ expect(disposeSplit).toHaveBeenCalledOnce();
247
+ original.focus();
248
+ key("keyup", { key: "c", code: "KeyC" });
249
+ expect(output).toEqual([
250
+ ["\x1b[57442;5u", "\x1b[57442;1:3u", "\x1b[98u", "\x1b[98;1:3u", "\x1b[99u", "\x1b[99;1:3u"],
251
+ [],
252
+ ]);
253
+ } finally {
254
+ window.removeEventListener("keydown", consumeShortcut, { capture: true });
255
+ composer.remove();
256
+ }
257
+});
apps/web/src/components/ThreadTerminalDrawer.tsx
+18
-49
@@ -1571,7 +1571,6 @@ export default function ThreadTerminalDrawer({
1571
const splitDirection =
1572
resolvedTerminalGroups[resolvedActiveGroupIndex]?.splitDirection ?? "horizontal";
1573
const hasTerminalSidebar = normalizedTerminalIds.length > 1;
1574
- const isSplitView = visibleTerminalIds.length > 1;
1574
const showGroupHeaders =
1575
resolvedTerminalGroups.length > 1 ||
1576
resolvedTerminalGroups.some((terminalGroup) => terminalGroup.terminalIds.length > 1);
@@ -1750,8 +1749,6 @@ export default function ThreadTerminalDrawer({
1749
);
1750
}
1751
1753
- const activeTerminalLaunchLocation = resolveTerminalLaunchLocation(resolvedActiveTerminalId);
1754
-
1752
return (
1753
<aside
1754
data-thread-terminal-drawer
@@ -1831,52 +1828,24 @@ export default function ThreadTerminalDrawer({
1828
)}
1829
>
1830
<div className="min-w-0 flex-1">
1834
- {isSplitView ? (
1835
- <TerminalSplitPanes
1836
- terminalIds={visibleTerminalIds}
1837
- splitDirection={splitDirection}
1838
- activeTerminalId={resolvedActiveTerminalId}
1839
- terminalLabelById={terminalLabelById}
1840
- resolveLaunchLocation={resolveTerminalLaunchLocation}
1841
- onActiveTerminalChange={onActiveTerminalChange}
1842
- onCloseTerminal={onCloseTerminal}
1843
- advancedTypography={advancedTypography}
1844
- threadRef={threadRef}
1845
- threadId={threadId}
1846
- onAddTerminalContext={onAddTerminalContext}
1847
- focusRequestId={focusRequestId}
1848
- visible={visible}
1849
- resizeEpoch={resizeEpoch}
1850
- drawerHeight={drawerHeight}
1851
- keybindings={keybindings}
1852
- />
1853
- ) : (
1854
- <div className="h-full">
1855
- <TerminalViewport
1856
- advancedTypography={advancedTypography}
1857
- key={resolvedActiveTerminalId}
1858
- threadRef={threadRef}
1859
- threadId={threadId}
1860
- terminalId={resolvedActiveTerminalId}
1861
- terminalLabel={terminalLabelById.get(resolvedActiveTerminalId) ?? "Terminal"}
1862
- cwd={activeTerminalLaunchLocation.cwd}
1863
- {...(activeTerminalLaunchLocation.worktreePath !== undefined
1864
- ? { worktreePath: activeTerminalLaunchLocation.worktreePath }
1865
- : {})}
1866
- {...(activeTerminalLaunchLocation.runtimeEnv
1867
- ? { runtimeEnv: activeTerminalLaunchLocation.runtimeEnv }
1868
- : {})}
1869
- onSessionExited={() => onCloseTerminal(resolvedActiveTerminalId)}
1870
- onAddTerminalContext={onAddTerminalContext}
1871
- focusRequestId={focusRequestId}
1872
- autoFocus
1873
- visible={visible}
1874
- resizeEpoch={resizeEpoch}
1875
- drawerHeight={drawerHeight}
1876
- keybindings={keybindings}
1877
- />
1878
- </div>
1879
- )}
1831
+ <TerminalSplitPanes
1832
+ terminalIds={visibleTerminalIds}
1833
+ splitDirection={splitDirection}
1834
+ activeTerminalId={resolvedActiveTerminalId}
1835
+ terminalLabelById={terminalLabelById}
1836
+ resolveLaunchLocation={resolveTerminalLaunchLocation}
1837
+ onActiveTerminalChange={onActiveTerminalChange}
1838
+ onCloseTerminal={onCloseTerminal}
1839
+ advancedTypography={advancedTypography}
1840
+ threadRef={threadRef}
1841
+ threadId={threadId}
1842
+ onAddTerminalContext={onAddTerminalContext}
1843
+ focusRequestId={focusRequestId}
1844
+ visible={visible}
1845
+ resizeEpoch={resizeEpoch}
1846
+ drawerHeight={drawerHeight}
1847
+ keybindings={keybindings}
1848
+ />
1849
</div>
1850
1851
{hasTerminalSidebar && (
apps/web/src/terminal/ghostty/surface.test.ts
+48
-1
@@ -55,11 +55,13 @@ describe("GhosttyTerminalSurface visibility", () => {
55
| "metaKey"
56
| "shiftKey"
57
| "isComposing"
58
+ | "repeat"
59
| "keyCode"
60
| "getModifierState"
61
>
62
> = {},
63
type = "keydown",
64
+ target: EventTarget = surface.input,
65
) {
66
const event = Object.assign(new Event(type, { cancelable: true }), {
67
key,
@@ -69,11 +71,12 @@ describe("GhosttyTerminalSurface visibility", () => {
71
metaKey: false,
72
shiftKey: false,
73
isComposing: false,
74
+ repeat: false,
75
keyCode: 0,
76
getModifierState: () => false,
77
...modifiers,
78
});
76
- surface.input.dispatchEvent(event);
79
+ target.dispatchEvent(event);
80
return event;
81
}
82
@@ -577,6 +580,50 @@ describe("GhosttyTerminalSurface visibility", () => {
580
expect(harness.onData.mock.calls[1]?.[0]).toContain(":3");
581
});
582
583
+ it("reports Kitty releases only for presses encoded by this surface", async () => {
584
+ const harness = createHarness();
585
+ const surface = await harness.create({ beforeKey: () => true });
586
+ surface.write("\x1b[>11u");
587
+
588
+ key(surface, "a", "KeyA");
589
+ key(surface, "a", "KeyA", {}, "keyup");
590
+ key(surface, "Control", "ControlLeft", { ctrlKey: true });
591
+ // App capture stops Ctrl+D before it reaches the terminal input.
592
+ key(surface, "d", "KeyD", { ctrlKey: true }, "keydown", window);
593
+ key(surface, "d", "KeyD", { ctrlKey: true }, "keyup");
594
+ key(surface, "Control", "ControlLeft", {}, "keyup");
595
+
596
+ expect(harness.onData.mock.calls.map(([data]) => data)).toEqual([
597
+ "\x1b[97u",
598
+ "\x1b[97;1:3u",
599
+ "\x1b[57442;5u",
600
+ "\x1b[57442;1:3u",
601
+ ]);
602
+ });
603
+
604
+ it("keeps a held Kitty press through focus changes and an app-consumed repeat", async () => {
605
+ const harness = createHarness();
606
+ let consumeRepeat = false;
607
+ const surface = await harness.create({ beforeKey: () => !consumeRepeat });
608
+ surface.write("\x1b[>11u");
609
+
610
+ key(surface, "a", "KeyA");
611
+ key(surface, "a", "KeyA", { repeat: true });
612
+ surface.input.dispatchEvent(new Event("blur"));
613
+ window.dispatchEvent(new Event("blur"));
614
+ window.dispatchEvent(new Event("focus"));
615
+ surface.input.dispatchEvent(new Event("focus"));
616
+ consumeRepeat = true;
617
+ key(surface, "a", "KeyA", { repeat: true });
618
+ key(surface, "a", "KeyA", {}, "keyup");
619
+
620
+ expect(harness.onData.mock.calls.map(([data]) => data)).toEqual([
621
+ "\x1b[97u",
622
+ "\x1b[97;1:2u",
623
+ "\x1b[97;1:3u",
624
+ ]);
625
+ });
626
+
627
it("honors host interception, IME candidates, and AltGr before local shortcuts", async () => {
628
vi.stubGlobal("navigator", { platform: "Linux x86_64" });
629
const harness = createHarness();
apps/web/src/terminal/ghostty/surface.ts
+23
-19
@@ -587,7 +587,8 @@ export class GhosttyTerminalSurface {
587
readonly ratio: number;
588
} | null = null;
589
private theme: GhosttyTheme;
590
- private readonly suppressedKeyCodes = new Set<string>();
590
+ // App capture can hide keydowns, so only encoded presses qualify for Kitty releases.
591
+ private readonly encodedKeyCodes = new Set<string>();
592
private pasteShortcutToken = 0;
593
// Chromium can deliver native paste after the clipboard read resolves.
594
// Suppress its duplicate only for the current shortcut gesture.
@@ -1058,18 +1059,12 @@ export class GhosttyTerminalSurface {
1059
}
1060
1061
private readonly onKeyDown = (event: KeyboardEvent) => {
1061
- // Presses handled outside the terminal must also swallow their release:
1062
- // beforeKey runs side effects (keybindings, navigation sends), so it cannot
1063
- // be consulted again on keyup, and Kitty report-event-types sessions would
1064
- // otherwise receive a release for a press the shell never saw.
1065
- if (isTerminalAltGraphText(event) || !this.options.beforeKey(event)) {
1066
- this.suppressedKeyCodes.add(event.code);
1067
- return;
1068
- }
1062
+ // A consumed repeat still owes the original encoded press a release.
1063
+ if (!event.repeat) this.encodedKeyCodes.delete(event.code);
1064
+ if (isTerminalAltGraphText(event) || !this.options.beforeKey(event)) return;
1065
// IME candidates belong to the textarea, even if the key also resembles
1066
// a local shortcut. Safari signals the initial composition with code 229.
1067
if (isTerminalCompositionKey(event, this.composing)) {
1072
- this.suppressedKeyCodes.add(event.code);
1068
return;
1069
}
1070
const mac = isMacPlatform(navigator.platform);
@@ -1082,7 +1077,6 @@ export class GhosttyTerminalSurface {
1077
if (selectAllShortcut) {
1078
event.preventDefault();
1079
event.stopPropagation();
1085
- this.suppressedKeyCodes.add(event.code);
1080
this.selectAll();
1081
return;
1082
}
@@ -1102,7 +1096,6 @@ export class GhosttyTerminalSurface {
1096
if (state !== null) {
1097
event.preventDefault();
1098
event.stopPropagation();
1105
- this.suppressedKeyCodes.add(event.code);
1099
const delta =
1100
event.key === "Home"
1101
? -state.offset
@@ -1153,11 +1146,9 @@ export class GhosttyTerminalSurface {
1146
});
1147
}
1148
}
1156
- this.suppressedKeyCodes.add(event.code);
1149
return;
1150
}
1151
if (isTerminalPasteShortcut(event)) {
1160
- this.suppressedKeyCodes.add(event.code);
1152
const gesture = { code: event.code, readText: "" };
1153
this.pasteShortcutGesture = gesture;
1154
const clipboard = navigator.clipboard;
@@ -1181,7 +1172,7 @@ export class GhosttyTerminalSurface {
1172
this.clearPrimedCopy();
1173
const data = this.core.encodeKey(event);
1174
if (data.length === 0) return;
1184
- this.suppressedKeyCodes.delete(event.code);
1175
+ this.encodedKeyCodes.add(event.code);
1176
event.preventDefault();
1177
event.stopPropagation();
1178
this.options.onData(data);
@@ -1197,7 +1188,7 @@ export class GhosttyTerminalSurface {
1188
) {
1189
this.pasteShortcutGesture = null;
1190
}
1200
- if (this.suppressedKeyCodes.delete(event.code)) return;
1191
+ if (!this.encodedKeyCodes.delete(event.code)) return;
1192
if (isTerminalCompositionKey(event, this.composing)) {
1193
return;
1194
}
@@ -1210,6 +1201,17 @@ export class GhosttyTerminalSurface {
1201
this.options.onData(data);
1202
};
1203
1204
+ // A release elsewhere must not leave a press for a later app shortcut to inherit.
1205
+ private readonly onWindowKeyUp = (event: KeyboardEvent) => {
1206
+ if (event.composedPath().includes(this.input)) return;
1207
+ this.encodedKeyCodes.delete(event.code);
1208
+ };
1209
+
1210
+ // A fresh press proves a release was missed; repeats can span a focus round trip.
1211
+ private readonly onWindowKeyDown = (event: KeyboardEvent) => {
1212
+ if (!event.repeat) this.encodedKeyCodes.delete(event.code);
1213
+ };
1214
+
1215
private readonly onFocus = () => {
1216
this.focused = true;
1217
this.cursorOn = true;
@@ -1220,9 +1222,6 @@ export class GhosttyTerminalSurface {
1222
this.focused = false;
1223
this.pasteShortcutGesture = null;
1224
this.refreshHoveredLink();
1223
- // Suppressions survive blur deliberately: a shortcut that moves focus (for
1224
- // example terminal-toggle) must still swallow its own keyup if focus comes
1225
- // back before release.
1225
this.cursorOn = true;
1226
this.requestRender();
1227
};
@@ -1774,6 +1773,9 @@ export class GhosttyTerminalSurface {
1773
private installEvents(): void {
1774
this.input.addEventListener("keydown", this.onKeyDown);
1775
this.input.addEventListener("keyup", this.onKeyUp);
1776
+ // App shortcuts stop propagation at window, so observe the same capture target.
1777
+ window.addEventListener("keyup", this.onWindowKeyUp, { capture: true });
1778
+ window.addEventListener("keydown", this.onWindowKeyDown, { capture: true });
1779
this.input.addEventListener("focus", this.onFocus);
1780
this.input.addEventListener("blur", this.onBlur);
1781
this.input.addEventListener("input", this.onInput);
@@ -1802,6 +1804,8 @@ export class GhosttyTerminalSurface {
1804
private removeEvents(): void {
1805
this.input.removeEventListener("keydown", this.onKeyDown);
1806
this.input.removeEventListener("keyup", this.onKeyUp);
1807
+ window.removeEventListener("keyup", this.onWindowKeyUp, { capture: true });
1808
+ window.removeEventListener("keydown", this.onWindowKeyDown, { capture: true });
1809
this.input.removeEventListener("focus", this.onFocus);
1810
this.input.removeEventListener("blur", this.onBlur);
1811
this.input.removeEventListener("input", this.onInput);