Skip to content

[BUG] Function Key Handling For Stagehand Agent #686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 65 additions & 28 deletions lib/handlers/agentHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -277,32 +277,69 @@ export class StagehandAgentHandler {
case "keypress": {
const { keys } = action;
if (Array.isArray(keys)) {
for (const key of keys) {
// Handle special keys
if (key.includes("ENTER")) {
await this.stagehandPage.page.keyboard.press("Enter");
} else if (key.includes("SPACE")) {
await this.stagehandPage.page.keyboard.press(" ");
} else if (key.includes("TAB")) {
await this.stagehandPage.page.keyboard.press("Tab");
} else if (key.includes("ESCAPE") || key.includes("ESC")) {
await this.stagehandPage.page.keyboard.press("Escape");
} else if (key.includes("BACKSPACE")) {
await this.stagehandPage.page.keyboard.press("Backspace");
} else if (key.includes("DELETE")) {
await this.stagehandPage.page.keyboard.press("Delete");
} else if (key.includes("ARROW_UP")) {
await this.stagehandPage.page.keyboard.press("ArrowUp");
} else if (key.includes("ARROW_DOWN")) {
await this.stagehandPage.page.keyboard.press("ArrowDown");
} else if (key.includes("ARROW_LEFT")) {
await this.stagehandPage.page.keyboard.press("ArrowLeft");
} else if (key.includes("ARROW_RIGHT")) {
await this.stagehandPage.page.keyboard.press("ArrowRight");
} else {
// For other keys, use the existing conversion
const playwrightKey = this.convertKeyName(key);
await this.stagehandPage.page.keyboard.press(playwrightKey);
// Check if CTRL or CMD is present in the keys
const hasModifier = keys.some(
(key) =>
key.includes("CTRL") ||
key.includes("CMD") ||
key.includes("COMMAND"),
);

if (hasModifier) {
// Handle key combination - press all keys simultaneously
try {
// Convert all keys first
const playwrightKeys = keys.map((key) => {
if (key.includes("CTRL")) return "Meta";
if (key.includes("CMD") || key.includes("COMMAND"))
return "Meta";
return this.convertKeyName(key);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Always mapping CTRL to Meta will break Windows functionality. Need to handle Windows platform separately instead of ignoring it.

Suggested change
const playwrightKeys = keys.map((key) => {
if (key.includes("CTRL")) return "Meta";
if (key.includes("CMD") || key.includes("COMMAND"))
return "Meta";
return this.convertKeyName(key);
const playwrightKeys = keys.map((key) => {
if (key.includes("CTRL")) return process.platform === "darwin" ? "Meta" : "Control";
if (key.includes("CMD") || key.includes("COMMAND"))
return "Meta";
return this.convertKeyName(key);

});

// Press all keys down in sequence
for (const key of playwrightKeys) {
await this.stagehandPage.page.keyboard.down(key);
}

// Small delay to ensure the combination is registered
await new Promise((resolve) => setTimeout(resolve, 100));

// Release all keys in reverse order
for (const key of playwrightKeys.reverse()) {
await this.stagehandPage.page.keyboard.up(key);
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Key release order should match press order for consistent behavior. Currently pressing in forward order but releasing in reverse.

} catch (error) {
throw error;
}
} else {
// Handle individual keys as before
for (const key of keys) {
// Handle special keys
if (key.includes("ENTER")) {
await this.stagehandPage.page.keyboard.press("Enter");
} else if (key.includes("SPACE")) {
await this.stagehandPage.page.keyboard.press(" ");
} else if (key.includes("TAB")) {
await this.stagehandPage.page.keyboard.press("Tab");
} else if (key.includes("ESCAPE") || key.includes("ESC")) {
await this.stagehandPage.page.keyboard.press("Escape");
} else if (key.includes("BACKSPACE")) {
await this.stagehandPage.page.keyboard.press("Backspace");
} else if (key.includes("DELETE")) {
await this.stagehandPage.page.keyboard.press("Delete");
} else if (key.includes("ARROW_UP")) {
await this.stagehandPage.page.keyboard.press("ArrowUp");
} else if (key.includes("ARROW_DOWN")) {
await this.stagehandPage.page.keyboard.press("ArrowDown");
} else if (key.includes("ARROW_LEFT")) {
await this.stagehandPage.page.keyboard.press("ArrowLeft");
} else if (key.includes("ARROW_RIGHT")) {
await this.stagehandPage.page.keyboard.press("ArrowRight");
} else {
// For other keys, use the existing conversion
const playwrightKey = this.convertKeyName(key);
await this.stagehandPage.page.keyboard.press(playwrightKey);
}
}
}
}
Expand Down Expand Up @@ -649,12 +686,12 @@ export class StagehandAgentHandler {
LEFT: "ArrowLeft",
RIGHT: "ArrowRight",
SHIFT: "Shift",
CONTROL: "Control",
CONTROL: process.platform === "darwin" ? "Meta" : "Control", // Use Meta on macOS
ALT: "Alt",
META: "Meta",
COMMAND: "Meta",
CMD: "Meta",
CTRL: "Control",
CTRL: process.platform === "darwin" ? "Meta" : "Control", // Use Meta on macOS
DELETE: "Delete",
HOME: "Home",
END: "End",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.