Lightpanda: the headless browser designed for AI and automation
Report Chrome's legacy keyCode, which and charCode for trusted key events
a.dmitryuk9673ae9c2c67
diff --git a/src/browser/webapi/event/KeyboardEvent.zig b/src/browser/webapi/event/KeyboardEvent.zigindex 6fcc0c838..7df7b4e64 100644--- a/src/browser/webapi/event/KeyboardEvent.zig+++ b/src/browser/webapi/event/KeyboardEvent.zig@@ -43,7 +43,9 @@ _meta_key: bool,_location: u32,_repeat: bool,_is_composing: bool,-// Legacy KeyboardEventInit values, reported as-is by synthetic events.+// Legacy KeyboardEventInit values, reported as-is by synthetic events. On a+// trusted keydown or keyup, `_key_code_init` is the virtual key code the user+// agent got with the key (CDP `windowsVirtualKeyCode`), 0 when there is none._char_code_init: u32 = 0,_key_code_init: u32 = 0,@@ -171,14 +173,13 @@ pub const Key = union(enum) {/// (https://www.w3.org/TR/uievents/#legacy-key-attributes). Returns 0 for/// keys without a defined fixed virtual key code.///- /// Spec note: for printable characters, `keyCode` is calculated from the- /// **unmodified** key value's uppercase ASCII. We don't track the- /// unmodified key, so we uppercase `_key` instead. This is exact for- /// letters (uppercase('t') == uppercase('T')) and digits, but for- /// shift-modified symbols (e.g., shift+1='!') it returns the modified- /// char's ASCII rather than the digit's keyCode. Callers needing- /// spec-strict behavior should pass `unmodifiedText` through CDP- /// `Input.dispatchKeyEvent` and use that instead.+ /// A printable character gets the virtual key code of the key that+ /// produces it on a US layout (Annex C, "Optionally fixed virtual key+ /// codes"): letters their uppercase ASCII, '!' the code of '1' (49), '.'+ /// and '>' 190. Never the character's own code: for punctuation that is+ /// another key's code ('.' is 46, Delete; '%' 37, ArrowLeft). Characters+ /// outside the US layout get 0. Only used when neither the client's+ /// virtual key code nor `code` gives the key, see `getKeyCode`.pub fn keyCode(self: Key) u32 {return switch (self) {// Modifier keys@@ -234,20 +235,35 @@ pub const Key = union(enum) {.F12 => 123,.standard => |s| {- if (s.len == 0) return 0;+ if (s.len != 1) return 0;const c = s[0];- // Letters: uppercase ASCII- if (c >= 'a' and c <= 'z') return c - 'a' + 'A';- if (c >= 'A' and c <= 'Z') return c;- // Digits: ASCII- if (c >= '0' and c <= '9') return c;- // Space: 32 — also returned via the ASCII fall-through below,- // but called out for clarity since it's the most common case.- if (c == ' ') return 32;- // Other ASCII chars (best-effort: legacy keyCode for symbols- // is platform-specific and depends on the unmodified key,- // which we don't track).- return c;+ return switch (c) {+ 'a'...'z' => c - 'a' + 'A',+ 'A'...'Z', '0'...'9', ' ' => c,+ // Shifted digits.+ ')' => '0',+ '!' => '1',+ '@' => '2',+ '#' => '3',+ '$' => '4',+ '%' => '5',+ '^' => '6',+ '&' => '7',+ '*' => '8',+ '(' => '9',+ ';', ':' => 186,+ '=', '+' => 187,+ ',', '<' => 188,+ '-', '_' => 189,+ '.', '>' => 190,+ '/', '?' => 191,+ '`', '~' => 192,+ '[', '{' => 219,+ '\\', '|' => 220,+ ']', '}' => 221,+ '\'', '"' => 222,+ else => 0,+ };},// Keys without a defined legacy keyCode@@ -255,6 +271,46 @@ pub const Key = union(enum) {};}+ /// Legacy `keyCode` of a printable key from its `KeyboardEvent.code`, the+ /// physical key, as Chrome's US layout gives it. Unlike `keyCode`, this+ /// tells the numpad keys from the main ones ('-' is 109 on the numpad, 189+ /// otherwise). Null for codes without a printable key.+ pub fn keyCodeFromCode(code: []const u8) ?u32 {+ if (code.len == 4 and std.mem.startsWith(u8, code, "Key")) {+ const c = code[3];+ return if (c >= 'A' and c <= 'Z') c else null;+ }+ if (code.len == 6 and std.mem.startsWith(u8, code, "Digit")) {+ const c = code[5];+ return if (c >= '0' and c <= '9') c else null;+ }+ if (code.len == 7 and std.mem.startsWith(u8, code, "Numpad")) {+ const c = code[6];+ return if (c >= '0' and c <= '9') 96 + @as(u32, c - '0') else null;+ }+ const map = std.StaticStringMap(u32).initComptime(.{+ .{ "Space", 32 },+ .{ "Semicolon", 186 },+ .{ "Equal", 187 },+ .{ "Comma", 188 },+ .{ "Minus", 189 },+ .{ "Period", 190 },+ .{ "Slash", 191 },+ .{ "Backquote", 192 },+ .{ "BracketLeft", 219 },+ .{ "Backslash", 220 },+ .{ "BracketRight", 221 },+ .{ "Quote", 222 },+ .{ "IntlBackslash", 226 },+ .{ "NumpadMultiply", 106 },+ .{ "NumpadAdd", 107 },+ .{ "NumpadSubtract", 109 },+ .{ "NumpadDecimal", 110 },+ .{ "NumpadDivide", 111 },+ });+ return map.get(code);+ }+/// Legacy `KeyboardEvent.charCode` value per UI Events spec § Annex C/// (https://www.w3.org/TR/uievents/#legacy-key-attributes). Returns the/// Unicode code point of the character produced by the key. Only@@ -262,7 +318,12 @@ pub const Key = union(enum) {pub fn charCode(self: Key) u32 {return switch (self) {.Enter => 13,- .standard => |s| if (s.len > 0) s[0] else 0,+ .standard => |s| {+ if (s.len == 0) return 0;+ const len = std.unicode.utf8ByteSequenceLength(s[0]) catch return 0;+ if (s.len < len) return 0;+ return std.unicode.utf8Decode(s[0..len]) catch 0;+ },else => 0,};}@@ -403,9 +464,21 @@ fn getCharCode(self: *const KeyboardEvent) u32 {// https://www.w3.org/TR/uievents/#dom-keyboardevent-keycode//// As with `charCode`, Chrome returns 0 for synthetic events.+//+// For a trusted `keypress` it is the character's code point, like `charCode`.+// For `keydown` and `keyup` it is the virtual key code the user agent got with+// the key (CDP `windowsVirtualKeyCode`), when there is one, as in Chrome.+// Otherwise it comes from `code`, then from `key`.pub fn getKeyCode(self: *const KeyboardEvent) u32 {+ const event = self._proto._proto;// Synthetic events report the (legacy) KeyboardEventInit value.- if (self._proto._proto._is_trusted == false) return self._key_code_init;+ if (event._is_trusted == false) return self._key_code_init;+ if (event._type_string.eql(comptime .wrap("keypress"))) return self._key.charCode();+ // For a trusted event, a virtual key code from the user agent.+ if (self._key_code_init != 0) return self._key_code_init;+ if (self._key == .standard) {+ if (Key.keyCodeFromCode(self._code)) |key_code| return key_code;+ }return self._key.keyCode();}@@ -512,6 +585,31 @@ test "KeyboardEvent: Key.keyCode mapping" {// Space.try testing.expectEqual(@as(u32, 32), Key.keyCode(.{ .standard = " " }));+ // Punctuation: the US layout key that produces it, never the character's+ // own code, which is another key's code ('.' is 46, Delete).+ try testing.expectEqual(@as(u32, 190), Key.keyCode(.{ .standard = "." }));+ try testing.expectEqual(@as(u32, 190), Key.keyCode(.{ .standard = ">" }));+ try testing.expectEqual(@as(u32, 188), Key.keyCode(.{ .standard = "," }));+ try testing.expectEqual(@as(u32, 189), Key.keyCode(.{ .standard = "-" }));+ try testing.expectEqual(@as(u32, 187), Key.keyCode(.{ .standard = "+" }));+ try testing.expectEqual(@as(u32, 186), Key.keyCode(.{ .standard = ";" }));+ try testing.expectEqual(@as(u32, 191), Key.keyCode(.{ .standard = "?" }));+ try testing.expectEqual(@as(u32, 192), Key.keyCode(.{ .standard = "~" }));+ try testing.expectEqual(@as(u32, 219), Key.keyCode(.{ .standard = "[" }));+ try testing.expectEqual(@as(u32, 220), Key.keyCode(.{ .standard = "|" }));+ try testing.expectEqual(@as(u32, 221), Key.keyCode(.{ .standard = "}" }));+ try testing.expectEqual(@as(u32, 222), Key.keyCode(.{ .standard = "'" }));+ try testing.expectEqual(@as(u32, 49), Key.keyCode(.{ .standard = "!" }));+ try testing.expectEqual(@as(u32, 53), Key.keyCode(.{ .standard = "%" }));+ try testing.expectEqual(@as(u32, 55), Key.keyCode(.{ .standard = "&" }));+ try testing.expectEqual(@as(u32, 57), Key.keyCode(.{ .standard = "(" }));+ try testing.expectEqual(@as(u32, 48), Key.keyCode(.{ .standard = ")" }));++ // Characters outside the US layout have no fixed virtual key code.+ try testing.expectEqual(@as(u32, 0), Key.keyCode(.{ .standard = "ж" }));+ try testing.expectEqual(@as(u32, 0), Key.keyCode(.{ .standard = "€" }));+ try testing.expectEqual(@as(u32, 0), Key.keyCode(.{ .standard = "ab" }));+// Modifier keys.try testing.expectEqual(@as(u32, 16), Key.keyCode(.Shift));try testing.expectEqual(@as(u32, 17), Key.keyCode(.Control));@@ -553,12 +651,38 @@ test "KeyboardEvent: Key.keyCode mapping" {try testing.expectEqual(@as(u32, 0), Key.keyCode(.{ .standard = "" }));}+test "KeyboardEvent: Key.keyCodeFromCode mapping" {+ try testing.expectEqual(@as(?u32, 65), Key.keyCodeFromCode("KeyA"));+ try testing.expectEqual(@as(?u32, 90), Key.keyCodeFromCode("KeyZ"));+ try testing.expectEqual(@as(?u32, 48), Key.keyCodeFromCode("Digit0"));+ try testing.expectEqual(@as(?u32, 57), Key.keyCodeFromCode("Digit9"));+ try testing.expectEqual(@as(?u32, 32), Key.keyCodeFromCode("Space"));+ try testing.expectEqual(@as(?u32, 190), Key.keyCodeFromCode("Period"));+ try testing.expectEqual(@as(?u32, 186), Key.keyCodeFromCode("Semicolon"));+ try testing.expectEqual(@as(?u32, 222), Key.keyCodeFromCode("Quote"));+ try testing.expectEqual(@as(?u32, 96), Key.keyCodeFromCode("Numpad0"));+ try testing.expectEqual(@as(?u32, 105), Key.keyCodeFromCode("Numpad9"));+ try testing.expectEqual(@as(?u32, 106), Key.keyCodeFromCode("NumpadMultiply"));+ try testing.expectEqual(@as(?u32, 109), Key.keyCodeFromCode("NumpadSubtract"));+ try testing.expectEqual(@as(?u32, 110), Key.keyCodeFromCode("NumpadDecimal"));+ try testing.expectEqual(@as(?u32, 111), Key.keyCodeFromCode("NumpadDivide"));+ try testing.expectEqual(@as(?u32, null), Key.keyCodeFromCode(""));+ try testing.expectEqual(@as(?u32, null), Key.keyCodeFromCode("Key"));+ try testing.expectEqual(@as(?u32, null), Key.keyCodeFromCode("KeyAB"));+ try testing.expectEqual(@as(?u32, null), Key.keyCodeFromCode("Unknown"));+}+test "KeyboardEvent: Key.charCode mapping" {- // Printable characters: Unicode code point of the first byte.+ // Printable characters: Unicode code point of the character.try testing.expectEqual(@as(u32, 97), Key.charCode(.{ .standard = "a" }));try testing.expectEqual(@as(u32, 65), Key.charCode(.{ .standard = "A" }));try testing.expectEqual(@as(u32, 48), Key.charCode(.{ .standard = "0" }));try testing.expectEqual(@as(u32, 32), Key.charCode(.{ .standard = " " }));+ try testing.expectEqual(@as(u32, 46), Key.charCode(.{ .standard = "." }));+ try testing.expectEqual(@as(u32, 1078), Key.charCode(.{ .standard = "ж" }));+ try testing.expectEqual(@as(u32, 1105), Key.charCode(.{ .standard = "ё" }));+ try testing.expectEqual(@as(u32, 8364), Key.charCode(.{ .standard = "€" }));+ try testing.expectEqual(@as(u32, 128512), Key.charCode(.{ .standard = "\u{1F600}" }));// Enter is the one named key that produces a charCode (\r = 13).try testing.expectEqual(@as(u32, 13), Key.charCode(.Enter));diff --git a/src/server/cdp/domains/input.zig b/src/server/cdp/domains/input.zigindex ca77c74ee..a54988615 100644--- a/src/server/cdp/domains/input.zig+++ b/src/server/cdp/domains/input.zig@@ -47,6 +47,8 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {code: ?[]const u8 = null,modifiers: u4 = 0,text: []const u8 = "",+ // Reported as keyCode and which by keydown and keyup, as in Chrome.+ windowsVirtualKeyCode: u32 = 0,// Many optional parameters are not implemented yet, see documentation url.const Type = enum {@@ -73,6 +75,7 @@ fn dispatchKeyEvent(cmd: *CDP.Command) !void {const opts: KeyboardEvent.Options = .{.key = if (params.key.len > 0) params.key else params.text,.code = params.code,+ .keyCode = params.windowsVirtualKeyCode,.altKey = modifiers.alt,.ctrlKey = modifiers.ctrl,.metaKey = modifiers.meta,@@ -2427,6 +2430,68 @@ test "cdp.input: dispatchKeyEvent char honors keypress and beforeinput vetoes" {, null)).isTrue());}+test "cdp.input: dispatchKeyEvent legacy keyCode, which and charCode" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .url = "mcp_actions.html" });+ const frame = bc.mainFrame().?;++ var ls: lp.js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();++ // Editors such as ProseMirror read keyCode on keydown and cancel what+ // they take for Delete (46) or an arrow key (37-40).+ _ = try ls.local.compileAndRun(+ \\const inp = document.getElementById('inp');+ \\inp.value = '';+ \\inp.focus();+ \\window.events = [];+ \\for (const t of ['keydown', 'keypress', 'keyup']) {+ \\ inp.addEventListener(t, (e) => window.events.push(t + ':' + e.keyCode + '/' + e.which + '/' + e.charCode));+ \\}+ \\inp.addEventListener('keydown', (e) => {+ \\ if (e.keyCode === 46 || (e.keyCode >= 37 && e.keyCode <= 40)) e.preventDefault();+ \\});+ , null);++ // Puppeteer and Playwright send windowsVirtualKeyCode with every key:+ // keydown and keyup report it, keypress reports the character's code point.+ try ctx.processMessage(.{ .id = 1, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = ".", .code = "Period", .text = ".", .windowsVirtualKeyCode = 190 } });+ try ctx.expectSentResult(null, .{ .id = 1 });+ try ctx.processMessage(.{ .id = 2, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyUp", .key = ".", .code = "Period", .windowsVirtualKeyCode = 190 } });+ try ctx.expectSentResult(null, .{ .id = 2 });+ try ctx.processMessage(.{ .id = 3, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "%", .code = "Digit5", .text = "%", .modifiers = 8, .windowsVirtualKeyCode = 53 } });+ try ctx.expectSentResult(null, .{ .id = 3 });+ try ctx.processMessage(.{ .id = 4, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyUp", .key = "%", .code = "Digit5", .modifiers = 8, .windowsVirtualKeyCode = 53 } });+ try ctx.expectSentResult(null, .{ .id = 4 });+ try testing.expect((try ls.local.compileAndRun(+ \\inp.value === '.%' && window.events.join() ===+ \\ 'keydown:190/190/0,keypress:46/46/46,keyup:190/190/0,' ++ \\ 'keydown:53/53/0,keypress:37/37/37,keyup:53/53/0'+ , null)).isTrue());++ // Without windowsVirtualKeyCode the code comes from `code`, then from the+ // US layout position of `key`, never from the character itself.+ _ = try ls.local.compileAndRun("inp.value = ''; window.events = [];", null);+ try ctx.processMessage(.{ .id = 5, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "-", .code = "NumpadSubtract", .text = "-" } });+ try ctx.expectSentResult(null, .{ .id = 5 });+ try ctx.processMessage(.{ .id = 6, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "(", .text = "(" } });+ try ctx.expectSentResult(null, .{ .id = 6 });+ try ctx.processMessage(.{ .id = 7, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "ж", .text = "ж" } });+ try ctx.expectSentResult(null, .{ .id = 7 });+ try ctx.processMessage(.{ .id = 8, .method = "Input.dispatchKeyEvent", .params = .{ .type = "keyDown", .key = "a", .text = "a" } });+ try ctx.expectSentResult(null, .{ .id = 8 });+ try testing.expect((try ls.local.compileAndRun(+ \\inp.value === '-(жa' && window.events.join() ===+ \\ 'keydown:109/109/0,keypress:45/45/45,' ++ \\ 'keydown:57/57/0,keypress:40/40/40,' ++ \\ 'keydown:0/0/0,keypress:1078/1078/1078,' ++ \\ 'keydown:65/65/0,keypress:97/97/97'+ , null)).isTrue());+}+test "cdp.input: dispatchKeyEvent cancelled keyDown suppresses its char" {var ctx = try testing.context();defer ctx.deinit();