Skip to content

Commit f4258c7

Browse files
authored
Merge pull request #36 from e2b-dev/hf-fixes-upd
Added human-friendly key mappings and updated readme
2 parents 886d285 + 5555624 commit f4258c7

File tree

5 files changed

+130
-4
lines changed

5 files changed

+130
-4
lines changed

.changeset/metal-spiders-trade.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@e2b/desktop-python": minor
3+
"@e2b/desktop": minor
4+
---
5+
6+
press key lowercase and readme changes

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ desktop.left_click()
174174
desktop.right_click()
175175
desktop.middle_click()
176176
desktop.scroll(10) # Scroll by the amount. Positive for up, negative for down.
177-
desktop.mouse_move(100, 200) # Move to x, y coordinates
177+
desktop.move_mouse(100, 200) # Move to x, y coordinates
178178
desktop.drag((100, 100), (200, 200)) # Drag using the mouse
179179
desktop.mouse_press("left") # Press the mouse button
180180
desktop.mouse_release("left") # Release the mouse button

packages/js-sdk/src/sandbox.ts

+64-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,63 @@ const MOUSE_BUTTONS = {
1919
middle: 2
2020
}
2121

22+
const KEYS = {
23+
"enter": "Return",
24+
"space": "space",
25+
"backspace": "BackSpace",
26+
"tab": "Tab",
27+
"escape": "Escape",
28+
"shift": "Shift_L",
29+
"shift_left": "Shift_L",
30+
"shift_right": "Shift_R",
31+
"control": "Control_L",
32+
"control_left": "Control_L",
33+
"control_right": "Control_R",
34+
"alt": "Alt_L",
35+
"alt_left": "Alt_L",
36+
"alt_right": "Alt_R",
37+
"super": "Super_L",
38+
"super_left": "Super_L",
39+
"super_right": "Super_R",
40+
"caps_lock": "Caps_Lock",
41+
"num_lock": "Num_Lock",
42+
"scroll_lock": "Scroll_Lock",
43+
"insert": "Insert",
44+
"delete": "Delete",
45+
"home": "Home",
46+
"end": "End",
47+
"page_up": "Page_Up",
48+
"page_down": "Page_Down",
49+
"up": "Up",
50+
"down": "Down",
51+
"left": "Left",
52+
"right": "Right",
53+
"f1": "F1",
54+
"f2": "F2",
55+
"f3": "F3",
56+
"f4": "F4",
57+
"f5": "F5",
58+
"f6": "F6",
59+
"f7": "F7",
60+
"f8": "F8",
61+
"f9": "F9",
62+
"f10": "F10",
63+
"f11": "F11",
64+
"f12": "F12",
65+
"print_screen": "Print",
66+
"pause": "Pause",
67+
"menu": "Menu",
68+
"meta": "Meta_L"
69+
}
70+
71+
function mapKey(key: string): string {
72+
const lowerKey = key.toLowerCase()
73+
if (lowerKey in KEYS) {
74+
return KEYS[lowerKey as keyof typeof KEYS]
75+
}
76+
return key
77+
}
78+
2279
/**
2380
* Configuration options for the Sandbox environment.
2481
* @interface SandboxOpts
@@ -396,8 +453,14 @@ export class Sandbox extends SandboxBase {
396453
* @param key - The key to press (e.g. "enter", "space", "backspace", etc.). Can be a single key or an array of keys.
397454
*/
398455
async press(key: string | string[]): Promise<void> {
456+
if (Array.isArray(key)) {
457+
key = key.map(mapKey).join('+')
458+
} else {
459+
key = mapKey(key)
460+
}
461+
399462
await this.commands.run(
400-
`xdotool key ${Array.isArray(key) ? key.join('+') : key}`, { envs: { DISPLAY: this.display } }
463+
`xdotool key ${key}`, { envs: { DISPLAY: this.display } }
401464
);
402465
}
403466

packages/python-sdk/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ desktop.left_click()
101101
desktop.right_click()
102102
desktop.middle_click()
103103
desktop.scroll(10) # Scroll by the amount. Positive for up, negative for down.
104-
desktop.mouse_move(100, 200) # Move to x, y coordinates
104+
desktop.move_mouse(100, 200) # Move to x, y coordinates
105105
desktop.drag((100, 100), (200, 200)) # Drag using the mouse
106106
desktop.mouse_press("left") # Press the mouse button
107107
desktop.mouse_release("left") # Release the mouse button

packages/python-sdk/e2b_desktop/main.py

+58-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,61 @@
1212
"middle": 2
1313
}
1414

15+
KEYS = {
16+
"enter": "Return",
17+
"space": "space",
18+
"backspace": "BackSpace",
19+
"tab": "Tab",
20+
"escape": "Escape",
21+
"shift": "Shift_L",
22+
"shift_left": "Shift_L",
23+
"shift_right": "Shift_R",
24+
"control": "Control_L",
25+
"control_left": "Control_L",
26+
"control_right": "Control_R",
27+
"alt": "Alt_L",
28+
"alt_left": "Alt_L",
29+
"alt_right": "Alt_R",
30+
"super": "Super_L",
31+
"super_left": "Super_L",
32+
"super_right": "Super_R",
33+
"caps_lock": "Caps_Lock",
34+
"num_lock": "Num_Lock",
35+
"scroll_lock": "Scroll_Lock",
36+
"insert": "Insert",
37+
"delete": "Delete",
38+
"home": "Home",
39+
"end": "End",
40+
"page_up": "Page_Up",
41+
"page_down": "Page_Down",
42+
"up": "Up",
43+
"down": "Down",
44+
"left": "Left",
45+
"right": "Right",
46+
"f1": "F1",
47+
"f2": "F2",
48+
"f3": "F3",
49+
"f4": "F4",
50+
"f5": "F5",
51+
"f6": "F6",
52+
"f7": "F7",
53+
"f8": "F8",
54+
"f9": "F9",
55+
"f10": "F10",
56+
"f11": "F11",
57+
"f12": "F12",
58+
"print_screen": "Print",
59+
"pause": "Pause",
60+
"menu": "Menu",
61+
"meta": "Meta_L"
62+
}
63+
64+
def map_key(key: str) -> str:
65+
lower_key = key.lower()
66+
if lower_key in KEYS:
67+
return KEYS[lower_key]
68+
return key
69+
1570
class _VNCServer:
1671
def __init__(self, desktop: "Sandbox") -> None:
1772
self.__vnc_handle: CommandHandle | None = None
@@ -371,7 +426,9 @@ def press(self, key: str | list[str]):
371426
:param key: The key to press (e.g. "enter", "space", "backspace", etc.).
372427
"""
373428
if isinstance(key, list):
374-
key = "+".join(key)
429+
key = "+".join(map_key(k) for k in key)
430+
else:
431+
key = map_key(key)
375432

376433
self.commands.run(f"xdotool key {key}", envs={"DISPLAY": self._display})
377434

0 commit comments

Comments
 (0)