Skip to content

The fastest browser for AI agents to run browser automation, built for sharing your logged-in browser state with your AI agents, like Codex or Claude Code, without disturbing you. Zero cost, zero config.

fix: map select-all keyboard command

Tachikoma925bfaee24d3
diff --git a/package/ego-browser/src/driver/keyboard.test.mjs b/package/ego-browser/src/driver/keyboard.test.mjs
new file mode 100644
index 0000000..de0fc40
--- /dev/null
+++ b/package/ego-browser/src/driver/keyboard.test.mjs
@@ -0,0 +1,114 @@
+import test from "node:test";
+import assert from "node:assert/strict";
+
+import { setOverrides } from "../../dist/src/state.js";
+import { pressKey } from "../../dist/src/driver/keyboard.js";
+
+test("pressKey maps Command+A to the selectAll editing command", async () => {
+ const calls = [];
+ const restore = setOverrides({
+ cdpOverride(method, params, sessionId) {
+ calls.push({ method, params, sessionId });
+ return {};
+ }
+ });
+ try {
+ await pressKey("a", 4);
+ } finally {
+ restore();
+ }
+
+ assert.equal(calls.length, 2);
+ assert.deepEqual(calls[0], {
+ method: "Input.dispatchKeyEvent",
+ sessionId: undefined,
+ params: {
+ type: "keyDown",
+ key: "a",
+ code: "KeyA",
+ modifiers: 4,
+ windowsVirtualKeyCode: 65,
+ nativeVirtualKeyCode: 65,
+ text: "a",
+ unmodifiedText: "a",
+ commands: ["selectAll"]
+ }
+ });
+ assert.deepEqual(calls[1].params, {
+ type: "keyUp",
+ key: "a",
+ code: "KeyA",
+ modifiers: 4,
+ windowsVirtualKeyCode: 65,
+ nativeVirtualKeyCode: 65
+ });
+});
+
+test("pressKey maps Control+A to the selectAll editing command", async () => {
+ const calls = [];
+ const restore = setOverrides({
+ cdpOverride(method, params, sessionId) {
+ calls.push({ method, params, sessionId });
+ return {};
+ }
+ });
+ try {
+ await pressKey("a", 2);
+ } finally {
+ restore();
+ }
+
+ assert.deepEqual(calls[0].params.commands, ["selectAll"]);
+});
+
+test("pressKey does not map modified Command+A variants to selectAll", async () => {
+ const calls = [];
+ const restore = setOverrides({
+ cdpOverride(method, params, sessionId) {
+ calls.push({ method, params, sessionId });
+ return {};
+ }
+ });
+ try {
+ await pressKey("a", 12);
+ } finally {
+ restore();
+ }
+
+ assert.equal(calls[0].params.commands, undefined);
+});
+
+test("pressKey leaves ordinary printable keys unchanged", async () => {
+ const calls = [];
+ const restore = setOverrides({
+ cdpOverride(method, params, sessionId) {
+ calls.push({ method, params, sessionId });
+ return {};
+ }
+ });
+ try {
+ await pressKey("x");
+ } finally {
+ restore();
+ }
+
+ assert.equal(calls.length, 2);
+ assert.deepEqual(calls[0].params, {
+ type: "keyDown",
+ key: "x",
+ code: "KeyX",
+ modifiers: 0,
+ windowsVirtualKeyCode: 88,
+ nativeVirtualKeyCode: 88,
+ text: "x",
+ unmodifiedText: "x"
+ });
+ assert.deepEqual(calls[1].params, {
+ type: "keyUp",
+ key: "x",
+ code: "KeyX",
+ modifiers: 0,
+ windowsVirtualKeyCode: 88,
+ nativeVirtualKeyCode: 88
+ });
+});
diff --git a/package/ego-browser/src/driver/keyboard.ts b/package/ego-browser/src/driver/keyboard.ts
index 7e22b8e..2b7a6d7 100644
--- a/package/ego-browser/src/driver/keyboard.ts
+++ b/package/ego-browser/src/driver/keyboard.ts
@@ -25,6 +25,8 @@ const KEYS = {
};
const PRINTABLE_CODE_RE = /^[A-Za-z0-9]$/;
+const CTRL_MODIFIER = 2;
+const META_MODIFIER = 4;
function keyDefinition(key) {
const special = KEYS[key];
@@ -39,16 +41,29 @@ function keyDefinition(key) {
return { vk, key, code, text: key };
}
+function editingCommandsForKey(key, modifiers) {
+ if ((modifiers === CTRL_MODIFIER || modifiers === META_MODIFIER) && key.toLowerCase() === "a") {
+ return ["selectAll"];
+ }
+ return undefined;
+}
+
/**
* Dispatch a key press through CDP.
* @param {string} key Key name such as Enter, Tab, ArrowLeft, or a single printable character.
- * @param {number} [modifiers=0] CDP modifier bitfield.
+ * @param {number} [modifiers=0] CDP modifier bitfield: Alt=1, Ctrl=2, Meta/Cmd=4, Shift=8.
* @returns {Promise<void>}
*/
export async function pressKey(key, modifiers = 0) {
const { vk, code, text } = keyDefinition(key);
const base = { key, code, modifiers, windowsVirtualKeyCode: vk, nativeVirtualKeyCode: vk };
- await cdp("Input.dispatchKeyEvent", { type: "keyDown", ...base, ...(text ? { text, unmodifiedText: text } : {}) });
+ const commands = editingCommandsForKey(key, modifiers);
+ await cdp("Input.dispatchKeyEvent", {
+ type: "keyDown",
+ ...base,
+ ...(text ? { text, unmodifiedText: text } : {}),
+ ...(commands ? { commands } : {})
+ });
await cdp("Input.dispatchKeyEvent", { type: "keyUp", ...base });
}