-
Notifications
You must be signed in to change notification settings - Fork 614
[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
emregucerr
wants to merge
4
commits into
browserbase:main
Choose a base branch
from
emregucerr:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+147
−46
Open
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
}); | ||
|
||
// 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); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} | ||
} | ||
|
@@ -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", | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.