Skip to content

Open-source Python library for AI browser agents, plus Browser Use Cloud: stealth browsers and a hosted agent API.

update Page.press() to include event code and windowsVirtualKeyCode

Reagan Hsu81933bb20c42
diff --git a/browser_use/actor/page.py b/browser_use/actor/page.py
index bed479e45..6428025bf 100644
--- a/browser_use/actor/page.py
+++ b/browser_use/actor/page.py
@@ -34,6 +34,161 @@ if TYPE_CHECKING:
from .mouse import Mouse
+def get_key_info(key: str) -> tuple[str, int | None]:
+ """Get the code and windowsVirtualKeyCode for a key.
+
+ Args:
+ key: Key name (e.g., 'Enter', 'ArrowUp', 'a', 'A')
+
+ Returns:
+ Tuple of (code, windowsVirtualKeyCode)
+
+ Reference: Windows Virtual Key Codes
+ https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
+ """
+ # Complete mapping of key names to (code, virtualKeyCode)
+ # Based on standard Windows Virtual Key Codes
+ key_map = {
+ # Navigation keys
+ 'Backspace': ('Backspace', 8),
+ 'Tab': ('Tab', 9),
+ 'Enter': ('Enter', 13),
+ 'Escape': ('Escape', 27),
+ 'Space': ('Space', 32),
+ ' ': ('Space', 32),
+ 'PageUp': ('PageUp', 33),
+ 'PageDown': ('PageDown', 34),
+ 'End': ('End', 35),
+ 'Home': ('Home', 36),
+ 'ArrowLeft': ('ArrowLeft', 37),
+ 'ArrowUp': ('ArrowUp', 38),
+ 'ArrowRight': ('ArrowRight', 39),
+ 'ArrowDown': ('ArrowDown', 40),
+ 'Insert': ('Insert', 45),
+ 'Delete': ('Delete', 46),
+ # Modifier keys
+ 'Shift': ('ShiftLeft', 16),
+ 'ShiftLeft': ('ShiftLeft', 16),
+ 'ShiftRight': ('ShiftRight', 16),
+ 'Control': ('ControlLeft', 17),
+ 'ControlLeft': ('ControlLeft', 17),
+ 'ControlRight': ('ControlRight', 17),
+ 'Alt': ('AltLeft', 18),
+ 'AltLeft': ('AltLeft', 18),
+ 'AltRight': ('AltRight', 18),
+ 'Meta': ('MetaLeft', 91),
+ 'MetaLeft': ('MetaLeft', 91),
+ 'MetaRight': ('MetaRight', 92),
+ # Function keys F1-F24
+ 'F1': ('F1', 112),
+ 'F2': ('F2', 113),
+ 'F3': ('F3', 114),
+ 'F4': ('F4', 115),
+ 'F5': ('F5', 116),
+ 'F6': ('F6', 117),
+ 'F7': ('F7', 118),
+ 'F8': ('F8', 119),
+ 'F9': ('F9', 120),
+ 'F10': ('F10', 121),
+ 'F11': ('F11', 122),
+ 'F12': ('F12', 123),
+ 'F13': ('F13', 124),
+ 'F14': ('F14', 125),
+ 'F15': ('F15', 126),
+ 'F16': ('F16', 127),
+ 'F17': ('F17', 128),
+ 'F18': ('F18', 129),
+ 'F19': ('F19', 130),
+ 'F20': ('F20', 131),
+ 'F21': ('F21', 132),
+ 'F22': ('F22', 133),
+ 'F23': ('F23', 134),
+ 'F24': ('F24', 135),
+ # Numpad keys
+ 'NumLock': ('NumLock', 144),
+ 'Numpad0': ('Numpad0', 96),
+ 'Numpad1': ('Numpad1', 97),
+ 'Numpad2': ('Numpad2', 98),
+ 'Numpad3': ('Numpad3', 99),
+ 'Numpad4': ('Numpad4', 100),
+ 'Numpad5': ('Numpad5', 101),
+ 'Numpad6': ('Numpad6', 102),
+ 'Numpad7': ('Numpad7', 103),
+ 'Numpad8': ('Numpad8', 104),
+ 'Numpad9': ('Numpad9', 105),
+ 'NumpadMultiply': ('NumpadMultiply', 106),
+ 'NumpadAdd': ('NumpadAdd', 107),
+ 'NumpadSubtract': ('NumpadSubtract', 109),
+ 'NumpadDecimal': ('NumpadDecimal', 110),
+ 'NumpadDivide': ('NumpadDivide', 111),
+ # Lock keys
+ 'CapsLock': ('CapsLock', 20),
+ 'ScrollLock': ('ScrollLock', 145),
+ # OEM/Punctuation keys (US keyboard layout)
+ 'Semicolon': ('Semicolon', 186),
+ ';': ('Semicolon', 186),
+ 'Equal': ('Equal', 187),
+ '=': ('Equal', 187),
+ 'Comma': ('Comma', 188),
+ ',': ('Comma', 188),
+ 'Minus': ('Minus', 189),
+ '-': ('Minus', 189),
+ 'Period': ('Period', 190),
+ '.': ('Period', 190),
+ 'Slash': ('Slash', 191),
+ '/': ('Slash', 191),
+ 'Backquote': ('Backquote', 192),
+ '`': ('Backquote', 192),
+ 'BracketLeft': ('BracketLeft', 219),
+ '[': ('BracketLeft', 219),
+ 'Backslash': ('Backslash', 220),
+ '\\': ('Backslash', 220),
+ 'BracketRight': ('BracketRight', 221),
+ ']': ('BracketRight', 221),
+ 'Quote': ('Quote', 222),
+ "'": ('Quote', 222),
+ # Media/Browser keys
+ 'AudioVolumeMute': ('AudioVolumeMute', 173),
+ 'AudioVolumeDown': ('AudioVolumeDown', 174),
+ 'AudioVolumeUp': ('AudioVolumeUp', 175),
+ 'MediaTrackNext': ('MediaTrackNext', 176),
+ 'MediaTrackPrevious': ('MediaTrackPrevious', 177),
+ 'MediaStop': ('MediaStop', 178),
+ 'MediaPlayPause': ('MediaPlayPause', 179),
+ 'BrowserBack': ('BrowserBack', 166),
+ 'BrowserForward': ('BrowserForward', 167),
+ 'BrowserRefresh': ('BrowserRefresh', 168),
+ 'BrowserStop': ('BrowserStop', 169),
+ 'BrowserSearch': ('BrowserSearch', 170),
+ 'BrowserFavorites': ('BrowserFavorites', 171),
+ 'BrowserHome': ('BrowserHome', 172),
+ # Additional common keys
+ 'Clear': ('Clear', 12),
+ 'Pause': ('Pause', 19),
+ 'Select': ('Select', 41),
+ 'Print': ('Print', 42),
+ 'Execute': ('Execute', 43),
+ 'PrintScreen': ('PrintScreen', 44),
+ 'Help': ('Help', 47),
+ 'ContextMenu': ('ContextMenu', 93),
+ }
+
+ if key in key_map:
+ return key_map[key]
+
+ # Handle alphanumeric keys dynamically
+ if len(key) == 1:
+ if key.isalpha():
+ # Letter keys: A-Z have VK codes 65-90
+ return (f'Key{key.upper()}', ord(key.upper()))
+ elif key.isdigit():
+ # Digit keys: 0-9 have VK codes 48-57 (same as ASCII)
+ return (f'Digit{key}', ord(key))
+
+ # Fallback: use the key name as code, no virtual key code
+ return (key, None)
+
+
class Page:
"""Page operations (tab or iframe)."""
@@ -218,28 +373,60 @@ class Page:
modifiers = parts[:-1]
main_key = parts[-1]
+ # Calculate modifier bitmask
+ modifier_value = 0
+ modifier_map = {'Alt': 1, 'Control': 2, 'Meta': 4, 'Shift': 8}
+ for mod in modifiers:
+ modifier_value |= modifier_map.get(mod, 0)
+
# Press modifier keys
for mod in modifiers:
- params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': mod}
+ code, vk_code = get_key_info(mod)
+ params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': mod, 'code': code}
+ if vk_code is not None:
+ params['windowsVirtualKeyCode'] = vk_code
await self._client.send.Input.dispatchKeyEvent(params, session_id=session_id)
- # Press main key
- main_down_params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': main_key}
+ # Press main key with modifiers bitmask
+ main_code, main_vk_code = get_key_info(main_key)
+ main_down_params: 'DispatchKeyEventParameters' = {
+ 'type': 'keyDown',
+ 'key': main_key,
+ 'code': main_code,
+ 'modifiers': modifier_value,
+ }
+ if main_vk_code is not None:
+ main_down_params['windowsVirtualKeyCode'] = main_vk_code
await self._client.send.Input.dispatchKeyEvent(main_down_params, session_id=session_id)
- main_up_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': main_key}
+ main_up_params: 'DispatchKeyEventParameters' = {
+ 'type': 'keyUp',
+ 'key': main_key,
+ 'code': main_code,
+ 'modifiers': modifier_value,
+ }
+ if main_vk_code is not None:
+ main_up_params['windowsVirtualKeyCode'] = main_vk_code
await self._client.send.Input.dispatchKeyEvent(main_up_params, session_id=session_id)
# Release modifier keys
for mod in reversed(modifiers):
- release_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': mod}
+ code, vk_code = get_key_info(mod)
+ release_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': mod, 'code': code}
+ if vk_code is not None:
+ release_params['windowsVirtualKeyCode'] = vk_code
await self._client.send.Input.dispatchKeyEvent(release_params, session_id=session_id)
else:
# Simple key press
- key_down_params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': key}
+ code, vk_code = get_key_info(key)
+ key_down_params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': key, 'code': code}
+ if vk_code is not None:
+ key_down_params['windowsVirtualKeyCode'] = vk_code
await self._client.send.Input.dispatchKeyEvent(key_down_params, session_id=session_id)
- key_up_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': key}
+ key_up_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': key, 'code': code}
+ if vk_code is not None:
+ key_up_params['windowsVirtualKeyCode'] = vk_code
await self._client.send.Input.dispatchKeyEvent(key_up_params, session_id=session_id)
async def set_viewport_size(self, width: int, height: int) -> None:
diff --git a/browser_use/browser/session.py b/browser_use/browser/session.py
index c0f942aba..0f544a062 100644
--- a/browser_use/browser/session.py
+++ b/browser_use/browser/session.py
@@ -122,11 +122,7 @@ class CDPSession(BaseModel):
result = await self.cdp_client.send.Target.attachToTarget(
params={
'targetId': self.target_id,
- 'flatten': True,
- 'filter': [ # type: ignore
- {'type': 'page', 'exclude': False},
- {'type': 'iframe', 'exclude': False},
- ],
+ 'flatten': True, #removed filter as a param because it doesn't exist at https://chromedevtools.github.io/devtools-protocol/tot/Target/#method-attachToTarget
}
)
self.session_id = result['sessionId']
diff --git a/browser_use/browser/watchdogs/default_action_watchdog.py b/browser_use/browser/watchdogs/default_action_watchdog.py
index 1ab26839b..12673d188 100644
--- a/browser_use/browser/watchdogs/default_action_watchdog.py
+++ b/browser_use/browser/watchdogs/default_action_watchdog.py
@@ -3,7 +3,9 @@
import asyncio
import json
import platform
+from typing import TYPE_CHECKING
+from browser_use.actor.page import get_key_info
from browser_use.browser.events import (
ClickElementEvent,
GetDropdownOptionsEvent,
@@ -23,6 +25,9 @@ from browser_use.browser.watchdog_base import BaseWatchdog
from browser_use.dom.service import EnhancedDOMTreeNode
from browser_use.observability import observe_debug
+if TYPE_CHECKING:
+ from cdp_use.cdp.input.commands import DispatchKeyEventParameters
+
# Import EnhancedDOMTreeNode and rebuild event models that have forward references to it
# This must be done after all imports are complete
ClickElementEvent.model_rebuild()
@@ -1535,155 +1540,123 @@ class DefaultActionWatchdog(BaseWatchdog):
"""Handle send keys request with CDP."""
cdp_session = await self.browser_session.get_or_create_cdp_session(focus=True)
try:
- # Parse key combination
- keys = event.keys.lower()
+ # Normalize key names from common aliases
+ key_aliases = {
+ 'ctrl': 'Control',
+ 'control': 'Control',
+ 'alt': 'Alt',
+ 'option': 'Alt',
+ 'meta': 'Meta',
+ 'cmd': 'Meta',
+ 'command': 'Meta',
+ 'shift': 'Shift',
+ 'enter': 'Enter',
+ 'return': 'Enter',
+ 'tab': 'Tab',
+ 'delete': 'Delete',
+ 'backspace': 'Backspace',
+ 'escape': 'Escape',
+ 'esc': 'Escape',
+ 'space': 'Space',
+ 'up': 'ArrowUp',
+ 'down': 'ArrowDown',
+ 'left': 'ArrowLeft',
+ 'right': 'ArrowRight',
+ 'pageup': 'PageUp',
+ 'pagedown': 'PageDown',
+ 'home': 'Home',
+ 'end': 'End',
+ }
- # Handle special key combinations
+ # Parse and normalize the key string
+ keys = event.keys
if '+' in keys:
- # Handle modifier keys
+ # Handle key combinations like "ctrl+a"
parts = keys.split('+')
- key = parts[-1]
-
- # Calculate modifier bits inline
- # CDP Modifier bits: Alt=1, Control=2, Meta/Command=4, Shift=8
- modifiers = 0
- for part in parts[:-1]:
- part_lower = part.lower()
- if part_lower in ['alt', 'option']:
- modifiers |= 1 # Alt
- elif part_lower in ['ctrl', 'control']:
- modifiers |= 2 # Control
- elif part_lower in ['meta', 'cmd', 'command']:
- modifiers |= 4 # Meta/Command
- elif part_lower in ['shift']:
- modifiers |= 8 # Shift
-
- # Send key with modifiers
- # Use rawKeyDown for non-text keys (like shortcuts)
- await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'rawKeyDown',
- 'key': key.capitalize() if len(key) == 1 else key,
- 'modifiers': modifiers,
- },
- session_id=cdp_session.session_id,
- )
- await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'keyUp',
- 'key': key.capitalize() if len(key) == 1 else key,
- 'modifiers': modifiers,
- },
- session_id=cdp_session.session_id,
- )
+ normalized_parts = []
+ for part in parts:
+ part_lower = part.strip().lower()
+ normalized = key_aliases.get(part_lower, part)
+ normalized_parts.append(normalized)
+ normalized_keys = '+'.join(normalized_parts)
else:
# Single key
- key_map = {
- 'enter': 'Enter',
- 'return': 'Enter',
- 'tab': 'Tab',
- 'delete': 'Delete',
- 'backspace': 'Backspace',
- 'escape': 'Escape',
- 'esc': 'Escape',
- 'space': ' ',
- 'up': 'ArrowUp',
- 'down': 'ArrowDown',
- 'left': 'ArrowLeft',
- 'right': 'ArrowRight',
- 'pageup': 'PageUp',
- 'pagedown': 'PageDown',
- 'home': 'Home',
- 'end': 'End',
- }
+ keys_lower = keys.strip().lower()
+ normalized_keys = key_aliases.get(keys_lower, keys)
+
+ # Handle key combinations like "Control+A"
+ if '+' in normalized_keys:
+ parts = normalized_keys.split('+')
+ modifiers = parts[:-1]
+ main_key = parts[-1]
+
+ # Calculate modifier bitmask
+ modifier_value = 0
+ modifier_map = {'Alt': 1, 'Control': 2, 'Meta': 4, 'Shift': 8}
+ for mod in modifiers:
+ modifier_value |= modifier_map.get(mod, 0)
+
+ # Press modifier keys
+ for mod in modifiers:
+ code, vk_code = get_key_info(mod)
+ params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': mod, 'code': code}
+ if vk_code is not None:
+ params['windowsVirtualKeyCode'] = vk_code
+ await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
+ params=params, session_id=cdp_session.session_id
+ )
- key = key_map.get(keys, keys)
-
- # Keys that need 3-step sequence (produce characters)
- keys_needing_char_event = ['enter', 'return', 'space']
-
- # Virtual key codes for proper key identification
- virtual_key_codes = {
- 'enter': 13,
- 'return': 13,
- 'tab': 9,
- 'escape': 27,
- 'esc': 27,
- 'space': 32,
- 'backspace': 8,
- 'delete': 46,
- 'up': 38,
- 'down': 40,
- 'left': 37,
- 'right': 39,
- 'home': 36,
- 'end': 35,
- 'pageup': 33,
- 'pagedown': 34,
+ # Press main key with modifiers bitmask
+ main_code, main_vk_code = get_key_info(main_key)
+ main_down_params: 'DispatchKeyEventParameters' = {
+ 'type': 'keyDown',
+ 'key': main_key,
+ 'code': main_code,
+ 'modifiers': modifier_value,
}
+ if main_vk_code is not None:
+ main_down_params['windowsVirtualKeyCode'] = main_vk_code
+ await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
+ params=main_down_params, session_id=cdp_session.session_id
+ )
- if keys in keys_needing_char_event:
- # 3-step sequence for keys that produce characters
- vk_code = virtual_key_codes.get(keys, 0)
- char_text = '\r' if keys in ['enter', 'return'] else ' ' if keys == 'space' else ''
+ main_up_params: 'DispatchKeyEventParameters' = {
+ 'type': 'keyUp',
+ 'key': main_key,
+ 'code': main_code,
+ 'modifiers': modifier_value,
+ }
+ if main_vk_code is not None:
+ main_up_params['windowsVirtualKeyCode'] = main_vk_code
+ await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
+ params=main_up_params, session_id=cdp_session.session_id
+ )
+ # Release modifier keys
+ for mod in reversed(modifiers):
+ code, vk_code = get_key_info(mod)
+ release_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': mod, 'code': code}
+ if vk_code is not None:
+ release_params['windowsVirtualKeyCode'] = vk_code
await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'rawKeyDown',
- 'windowsVirtualKeyCode': vk_code,
- 'code': key_map.get(keys, keys),
- 'key': key_map.get(keys, keys),
- },
- session_id=cdp_session.session_id,
- )
- await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={'type': 'char', 'text': char_text, 'unmodifiedText': char_text},
- session_id=cdp_session.session_id,
- )
- await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'keyUp',
- 'windowsVirtualKeyCode': vk_code,
- 'code': key_map.get(keys, keys),
- 'key': key_map.get(keys, keys),
- },
- session_id=cdp_session.session_id,
+ params=release_params, session_id=cdp_session.session_id
)
- else:
- # 2-step sequence for other keys
- key_type = 'rawKeyDown' if keys in key_map else 'keyDown'
- vk_code = virtual_key_codes.get(keys)
+ else:
+ # Simple key press
+ code, vk_code = get_key_info(normalized_keys)
+ key_down_params: 'DispatchKeyEventParameters' = {'type': 'keyDown', 'key': normalized_keys, 'code': code}
+ if vk_code is not None:
+ key_down_params['windowsVirtualKeyCode'] = vk_code
+ await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
+ params=key_down_params, session_id=cdp_session.session_id
+ )
- if vk_code:
- # Special keys with virtual key codes
- await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': key_type,
- 'key': key,
- 'windowsVirtualKeyCode': vk_code,
- 'code': key_map.get(keys, keys),
- },
- session_id=cdp_session.session_id,
- )
- await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={
- 'type': 'keyUp',
- 'key': key,
- 'windowsVirtualKeyCode': vk_code,
- 'code': key_map.get(keys, keys),
- },
- session_id=cdp_session.session_id,
- )
- else:
- # Regular characters without virtual key codes
- await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={'type': key_type, 'key': key},
- session_id=cdp_session.session_id,
- )
- await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
- params={'type': 'keyUp', 'key': key},
- session_id=cdp_session.session_id,
- )
+ key_up_params: 'DispatchKeyEventParameters' = {'type': 'keyUp', 'key': normalized_keys, 'code': code}
+ if vk_code is not None:
+ key_up_params['windowsVirtualKeyCode'] = vk_code
+ await cdp_session.cdp_client.send.Input.dispatchKeyEvent(
+ params=key_up_params, session_id=cdp_session.session_id
+ )
self.logger.info(f'⌨️ Sent keys: {event.keys}')