Skip to content

Commit cfbdaf4

Browse files
juliusmarmingecodex
andcommitted
Fix preview keyboard dispatch
Build Chromium-compatible key packets with virtual key codes, distinguish text and raw keydown events, and guarantee keyup cleanup. Add focused coverage for named, printable, shortcut, and shifted keys. Co-authored-by: codex <[email protected]>
1 parent af87374 commit cfbdaf4

4 files changed

Lines changed: 247 additions & 41 deletions

File tree

‎apps/desktop/src/preview/Manager.test.ts‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,11 +685,14 @@ describe("PreviewManager", () => {
685685
if (
686686
failKeyDown &&
687687
method === "Input.dispatchKeyEvent" &&
688-
params?.["type"] === "keyDown"
688+
(params?.["type"] === "keyDown" || params?.["type"] === "rawKeyDown")
689689
) {
690690
throw new Error("key dispatch failed");
691691
}
692-
if (method === "Input.dispatchKeyEvent" && params?.["type"] === "keyDown") {
692+
if (
693+
method === "Input.dispatchKeyEvent" &&
694+
(params?.["type"] === "keyDown" || params?.["type"] === "rawKeyDown")
695+
) {
693696
humanInput?.(
694697
{},
695698
{
@@ -799,8 +802,9 @@ describe("PreviewManager", () => {
799802
key: "y",
800803
code: "KeyY",
801804
modifiers: 0,
802-
text: "y",
803-
unmodifiedText: "y",
805+
windowsVirtualKeyCode: 89,
806+
location: 0,
807+
isKeypad: false,
804808
});
805809
expect(sendCommand).toHaveBeenCalledWith("Emulation.setFocusEmulationEnabled", {
806810
enabled: false,
@@ -819,7 +823,11 @@ describe("PreviewManager", () => {
819823
expect(sendCommand).toHaveBeenCalledWith("Input.dispatchKeyEvent", {
820824
type: "keyDown",
821825
key: "!",
826+
code: "Digit1",
822827
modifiers: 0,
828+
windowsVirtualKeyCode: 49,
829+
location: 0,
830+
isKeypad: false,
823831
text: "!",
824832
unmodifiedText: "!",
825833
});

‎apps/desktop/src/preview/Manager.ts‎

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ import {
6262
} from "./GuestProtocol.ts";
6363
import { isPreviewAnnotationPayload } from "./PickedElementPayload.ts";
6464
import { playwrightInjectedRuntimeInstallExpression } from "./PlaywrightInjectedRuntime.ts";
65+
import { makePreviewAutomationKeySequence } from "./PreviewKeyboard.ts";
6566

6667
export type PreviewNavStatus =
6768
| { kind: "Idle" }
@@ -373,13 +374,6 @@ const inputSignalsMatch = (left: PreviewInputSignal, right: PreviewInputSignal):
373374
);
374375
};
375376

376-
const previewAutomationKeyCode = (key: string): string | undefined => {
377-
if (/^[a-z]$/i.test(key)) return `Key${key.toUpperCase()}`;
378-
if (/^[0-9]$/.test(key)) return `Digit${key}`;
379-
if (key === " ") return "Space";
380-
return key.length > 1 ? key : undefined;
381-
};
382-
383377
const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function* (
384378
artifactDirectory: string,
385379
) {
@@ -2194,37 +2188,15 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
21942188
sendCleanup: SendCommand,
21952189
) {
21962190
yield* prepareAutomationInput(send, false);
2197-
const modifiers = (input.modifiers ?? []).reduce((value, modifier) => {
2198-
switch (modifier) {
2199-
case "Alt":
2200-
return value | 1;
2201-
case "Control":
2202-
return value | 2;
2203-
case "Meta":
2204-
return value | 4;
2205-
case "Shift":
2206-
return value | 8;
2207-
}
2208-
}, 0);
2209-
const key = input.key;
2210-
const code = previewAutomationKeyCode(key);
2211-
const text = key.length === 1 ? key : undefined;
2212-
const params = {
2213-
key,
2214-
...(code === undefined ? {} : { code }),
2215-
modifiers,
2216-
...(text ? { text, unmodifiedText: text } : {}),
2217-
};
2191+
const keySequence = makePreviewAutomationKeySequence(input);
22182192
const previouslyFocused = yield* attempt(
22192193
{ operation: "automationPress.getFocusedWebContents", tabId, webContentsId: wc.id },
22202194
() => webContents.getFocusedWebContents(),
22212195
);
22222196
let keyDownAttempted = false;
22232197
const releaseInput = Effect.gen(function* () {
22242198
if (keyDownAttempted) {
2225-
yield* sendCleanup("Input.dispatchKeyEvent", { type: "keyUp", ...params }).pipe(
2226-
Effect.ignore,
2227-
);
2199+
yield* sendCleanup("Input.dispatchKeyEvent", keySequence.keyUp).pipe(Effect.ignore);
22282200
}
22292201
yield* sendCleanup("Emulation.setFocusEmulationEnabled", { enabled: false }).pipe(
22302202
Effect.ignore,
@@ -2252,13 +2224,9 @@ const makeNativeOperations = Effect.fn("PreviewManager.makeOperations")(function
22522224
);
22532225
yield* send("Page.bringToFront");
22542226
yield* send("Emulation.setFocusEmulationEnabled", { enabled: true });
2255-
yield* expectAgentInput(tabId, {
2256-
kind: "key",
2257-
key,
2258-
...(code === undefined ? {} : { code }),
2259-
});
2227+
yield* expectAgentInput(tabId, keySequence.signal);
22602228
keyDownAttempted = true;
2261-
yield* send("Input.dispatchKeyEvent", { type: "keyDown", ...params });
2229+
yield* send("Input.dispatchKeyEvent", keySequence.keyDown);
22622230
}).pipe(Effect.ensuring(releaseInput));
22632231
});
22642232

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { makePreviewAutomationKeySequence } from "./PreviewKeyboard.ts";
4+
5+
describe("preview keyboard packets", () => {
6+
it("includes the Chromium virtual key code and Enter text", () => {
7+
expect(makePreviewAutomationKeySequence({ key: "Enter" })).toEqual({
8+
keyDown: {
9+
type: "keyDown",
10+
key: "Enter",
11+
code: "Enter",
12+
modifiers: 0,
13+
windowsVirtualKeyCode: 13,
14+
location: 0,
15+
isKeypad: false,
16+
text: "\r",
17+
unmodifiedText: "\r",
18+
},
19+
keyUp: {
20+
type: "keyUp",
21+
key: "Enter",
22+
code: "Enter",
23+
modifiers: 0,
24+
windowsVirtualKeyCode: 13,
25+
location: 0,
26+
isKeypad: false,
27+
},
28+
signal: { kind: "key", key: "Enter", code: "Enter" },
29+
});
30+
});
31+
32+
it("dispatches printable keys as text key-down events", () => {
33+
const sequence = makePreviewAutomationKeySequence({ key: "z" });
34+
expect(sequence.keyDown).toMatchObject({
35+
type: "keyDown",
36+
key: "z",
37+
code: "KeyZ",
38+
windowsVirtualKeyCode: 90,
39+
text: "z",
40+
});
41+
expect(sequence.keyUp).not.toHaveProperty("text");
42+
});
43+
44+
it("suppresses text and uses raw key-down for shortcuts", () => {
45+
expect(makePreviewAutomationKeySequence({ key: "a", modifiers: ["Meta"] }).keyDown).toEqual({
46+
type: "rawKeyDown",
47+
key: "a",
48+
code: "KeyA",
49+
modifiers: 4,
50+
windowsVirtualKeyCode: 65,
51+
location: 0,
52+
isKeypad: false,
53+
});
54+
});
55+
56+
it("resolves shifted printable keys to their browser values", () => {
57+
const sequence = makePreviewAutomationKeySequence({ key: "1", modifiers: ["Shift"] });
58+
expect(sequence.keyDown).toMatchObject({
59+
key: "!",
60+
code: "Digit1",
61+
modifiers: 8,
62+
windowsVirtualKeyCode: 49,
63+
text: "!",
64+
});
65+
expect(sequence.signal).toEqual({ kind: "key", key: "!", code: "Digit1" });
66+
});
67+
});
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
import type { PreviewAutomationPressInput } from "@t3tools/contracts";
2+
3+
interface KeyDefinition {
4+
readonly code: string;
5+
readonly key: string;
6+
readonly keyCode: number;
7+
readonly text?: string;
8+
readonly location?: number;
9+
readonly shiftedKey?: string;
10+
}
11+
12+
export interface PreviewAutomationKeyEvent {
13+
readonly [key: string]: unknown;
14+
readonly type: "keyDown" | "rawKeyDown" | "keyUp";
15+
readonly key: string;
16+
readonly code: string;
17+
readonly modifiers: number;
18+
readonly windowsVirtualKeyCode: number;
19+
readonly location: number;
20+
readonly isKeypad: boolean;
21+
readonly text?: string;
22+
readonly unmodifiedText?: string;
23+
}
24+
25+
export interface PreviewAutomationKeySequence {
26+
readonly keyDown: PreviewAutomationKeyEvent;
27+
readonly keyUp: PreviewAutomationKeyEvent;
28+
readonly signal: {
29+
readonly kind: "key";
30+
readonly key: string;
31+
readonly code: string;
32+
};
33+
}
34+
35+
const NAMED_KEYS: Readonly<Record<string, KeyDefinition>> = {
36+
Escape: { code: "Escape", key: "Escape", keyCode: 27 },
37+
Backspace: { code: "Backspace", key: "Backspace", keyCode: 8 },
38+
Tab: { code: "Tab", key: "Tab", keyCode: 9 },
39+
Enter: { code: "Enter", key: "Enter", keyCode: 13, text: "\r" },
40+
Shift: { code: "ShiftLeft", key: "Shift", keyCode: 16, location: 1 },
41+
Control: { code: "ControlLeft", key: "Control", keyCode: 17, location: 1 },
42+
Alt: { code: "AltLeft", key: "Alt", keyCode: 18, location: 1 },
43+
Meta: { code: "MetaLeft", key: "Meta", keyCode: 91, location: 1 },
44+
CapsLock: { code: "CapsLock", key: "CapsLock", keyCode: 20 },
45+
Space: { code: "Space", key: " ", keyCode: 32, text: " " },
46+
PageUp: { code: "PageUp", key: "PageUp", keyCode: 33 },
47+
PageDown: { code: "PageDown", key: "PageDown", keyCode: 34 },
48+
End: { code: "End", key: "End", keyCode: 35 },
49+
Home: { code: "Home", key: "Home", keyCode: 36 },
50+
ArrowLeft: { code: "ArrowLeft", key: "ArrowLeft", keyCode: 37 },
51+
ArrowUp: { code: "ArrowUp", key: "ArrowUp", keyCode: 38 },
52+
ArrowRight: { code: "ArrowRight", key: "ArrowRight", keyCode: 39 },
53+
ArrowDown: { code: "ArrowDown", key: "ArrowDown", keyCode: 40 },
54+
Insert: { code: "Insert", key: "Insert", keyCode: 45 },
55+
Delete: { code: "Delete", key: "Delete", keyCode: 46 },
56+
};
57+
58+
const PRINTABLE_KEYS: ReadonlyArray<KeyDefinition> = [
59+
{ code: "Backquote", key: "`", shiftedKey: "~", keyCode: 192 },
60+
{ code: "Digit1", key: "1", shiftedKey: "!", keyCode: 49 },
61+
{ code: "Digit2", key: "2", shiftedKey: "@", keyCode: 50 },
62+
{ code: "Digit3", key: "3", shiftedKey: "#", keyCode: 51 },
63+
{ code: "Digit4", key: "4", shiftedKey: "$", keyCode: 52 },
64+
{ code: "Digit5", key: "5", shiftedKey: "%", keyCode: 53 },
65+
{ code: "Digit6", key: "6", shiftedKey: "^", keyCode: 54 },
66+
{ code: "Digit7", key: "7", shiftedKey: "&", keyCode: 55 },
67+
{ code: "Digit8", key: "8", shiftedKey: "*", keyCode: 56 },
68+
{ code: "Digit9", key: "9", shiftedKey: "(", keyCode: 57 },
69+
{ code: "Digit0", key: "0", shiftedKey: ")", keyCode: 48 },
70+
{ code: "Minus", key: "-", shiftedKey: "_", keyCode: 189 },
71+
{ code: "Equal", key: "=", shiftedKey: "+", keyCode: 187 },
72+
{ code: "Backslash", key: "\\", shiftedKey: "|", keyCode: 220 },
73+
{ code: "BracketLeft", key: "[", shiftedKey: "{", keyCode: 219 },
74+
{ code: "BracketRight", key: "]", shiftedKey: "}", keyCode: 221 },
75+
{ code: "Semicolon", key: ";", shiftedKey: ":", keyCode: 186 },
76+
{ code: "Quote", key: "'", shiftedKey: '"', keyCode: 222 },
77+
{ code: "Comma", key: ",", shiftedKey: "<", keyCode: 188 },
78+
{ code: "Period", key: ".", shiftedKey: ">", keyCode: 190 },
79+
{ code: "Slash", key: "/", shiftedKey: "?", keyCode: 191 },
80+
];
81+
82+
const modifierMask = (modifiers: PreviewAutomationPressInput["modifiers"]): number =>
83+
(modifiers ?? []).reduce((value, modifier) => {
84+
switch (modifier) {
85+
case "Alt":
86+
return value | 1;
87+
case "Control":
88+
return value | 2;
89+
case "Meta":
90+
return value | 4;
91+
case "Shift":
92+
return value | 8;
93+
}
94+
}, 0);
95+
96+
function resolveKeyDefinition(input: PreviewAutomationPressInput): KeyDefinition {
97+
const named = NAMED_KEYS[input.key];
98+
if (named) return named;
99+
100+
const functionKey = /^F([1-9]|1[0-2])$/.exec(input.key);
101+
if (functionKey) {
102+
const number = Number(functionKey[1]);
103+
return { code: input.key, key: input.key, keyCode: 111 + number };
104+
}
105+
106+
if (/^[a-z]$/i.test(input.key)) {
107+
const upper = input.key.toUpperCase();
108+
const shifted = input.modifiers?.includes("Shift") ?? false;
109+
const key = shifted || input.key === upper ? upper : input.key;
110+
return { code: `Key${upper}`, key, keyCode: upper.charCodeAt(0), text: key };
111+
}
112+
113+
const printable = PRINTABLE_KEYS.find(
114+
(definition) => definition.key === input.key || definition.shiftedKey === input.key,
115+
);
116+
if (printable) {
117+
const shifted = input.modifiers?.includes("Shift") ?? false;
118+
const key =
119+
printable.shiftedKey && (shifted || input.key === printable.shiftedKey)
120+
? printable.shiftedKey
121+
: printable.key;
122+
return { ...printable, key, text: key };
123+
}
124+
125+
return {
126+
code: input.key.length > 1 ? input.key : "",
127+
key: input.key,
128+
keyCode: 0,
129+
...(input.key.length === 1 ? { text: input.key } : {}),
130+
};
131+
}
132+
133+
/**
134+
* Build Chromium CDP key packets using the same required fields and down-event
135+
* choice as Playwright's pinned Chromium keyboard implementation.
136+
*/
137+
export function makePreviewAutomationKeySequence(
138+
input: PreviewAutomationPressInput,
139+
): PreviewAutomationKeySequence {
140+
const definition = resolveKeyDefinition(input);
141+
const modifiers = modifierMask(input.modifiers);
142+
const suppressText = input.modifiers?.some((modifier) => modifier !== "Shift") ?? false;
143+
const text = suppressText ? "" : (definition.text ?? "");
144+
const location = definition.location ?? 0;
145+
const shared = {
146+
key: definition.key,
147+
code: definition.code,
148+
modifiers,
149+
windowsVirtualKeyCode: definition.keyCode,
150+
location,
151+
isKeypad: location === 3,
152+
};
153+
154+
return {
155+
keyDown: {
156+
type: text ? "keyDown" : "rawKeyDown",
157+
...shared,
158+
...(text ? { text, unmodifiedText: text } : {}),
159+
},
160+
keyUp: { type: "keyUp", ...shared },
161+
signal: { kind: "key", key: definition.key, code: definition.code },
162+
};
163+
}

0 commit comments

Comments
 (0)