Skip to content

Commit 8b1c87e

Browse files
authored
Merge pull request #33 from e2b-dev/add-more-cua-methods
Add mouse_press and mouse_release for additional controls
2 parents 8df07e2 + 373f9fe commit 8b1c87e

File tree

5 files changed

+53
-5
lines changed

5 files changed

+53
-5
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,8 @@ desktop.middle_click()
176176
desktop.scroll(10) # Scroll by the amount. Positive for up, negative for down.
177177
desktop.mouse_move(100, 200) # Move to x, y coordinates
178178
desktop.drag((100, 100), (200, 200)) # Drag using the mouse
179+
desktop.mouse_press("left") # Press the mouse button
180+
desktop.mouse_release("left") # Release the mouse button
179181
```
180182

181183
**JavaScript**
@@ -192,6 +194,8 @@ await desktop.middleClick()
192194
await desktop.scroll(10) // Scroll by the amount. Positive for up, negative for down.
193195
await desktop.moveMouse(100, 200) // Move to x, y coordinates
194196
await desktop.drag([100, 100], [200, 200]) // Drag using the mouse
197+
await desktop.mousePress("left") // Press the mouse button
198+
await desktop.mouseRelease("left") // Release the mouse button
195199
```
196200

197201
### Keyboard control

packages/js-sdk/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,8 @@ await desktop.middleClick()
106106
await desktop.scroll(10) // Scroll by the amount. Positive for up, negative for down.
107107
await desktop.moveMouse(100, 200) // Move to x, y coordinates
108108
await desktop.drag([100, 100], [200, 200]) // Drag using the mouse
109+
await desktop.mousePress("left") // Press the mouse button
110+
await desktop.mouseRelease("left") // Release the mouse button
109111
```
110112

111113
### Keyboard control

packages/js-sdk/src/sandbox.ts

+24-4
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ interface ScreenSize {
1313
height: number;
1414
}
1515

16+
const MOUSE_BUTTONS = {
17+
left: 1,
18+
right: 3,
19+
middle: 2
20+
}
21+
1622
/**
1723
* Configuration options for the Sandbox environment.
1824
* @interface SandboxOpts
@@ -280,6 +286,20 @@ export class Sandbox extends SandboxBase {
280286
);
281287
}
282288

289+
/**
290+
* Press the mouse button.
291+
*/
292+
async mousePress(button: 'left' | 'right' | 'middle' = 'left'): Promise<void> {
293+
await this.commands.run(`xdotool mousedown ${MOUSE_BUTTONS[button]}`, { envs: { DISPLAY: this.display } });
294+
}
295+
296+
/**
297+
* Release the mouse button.
298+
*/
299+
async mouseRelease(button: 'left' | 'right' | 'middle' = 'left'): Promise<void> {
300+
await this.commands.run(`xdotool mouseup ${MOUSE_BUTTONS[button]}`, { envs: { DISPLAY: this.display } });
301+
}
302+
283303
/**
284304
* Get the current cursor position.
285305
* @returns A object with the x and y coordinates
@@ -387,10 +407,10 @@ export class Sandbox extends SandboxBase {
387407
* @param to - The ending position.
388408
*/
389409
async drag([x1, y1]: [number, number], [x2, y2]: [number, number]): Promise<void> {
390-
await this.commands.run(
391-
`xdotool mousemove ${x1} ${y1} && xdotool mousedown 1 && xdotool mousemove ${x2} ${y2} && xdotool mouseup 1`,
392-
{ envs: { DISPLAY: this.display } }
393-
);
410+
await this.moveMouse(x1, y1);
411+
await this.mousePress();
412+
await this.moveMouse(x2, y2);
413+
await this.mouseRelease();
394414
}
395415

396416
/**

packages/python-sdk/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,8 @@ desktop.middle_click()
103103
desktop.scroll(10) # Scroll by the amount. Positive for up, negative for down.
104104
desktop.mouse_move(100, 200) # Move to x, y coordinates
105105
desktop.drag((100, 100), (200, 200)) # Drag using the mouse
106+
desktop.mouse_press("left") # Press the mouse button
107+
desktop.mouse_release("left") # Release the mouse button
106108
```
107109

108110
### Keyboard control

packages/python-sdk/e2b_desktop/main.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
from e2b import Sandbox as SandboxBase, CommandHandle, CommandResult, TimeoutException, CommandExitException
88

9+
MOUSE_BUTTONS = {
10+
"left": 1,
11+
"right": 3,
12+
"middle": 2
13+
}
914

1015
class _VNCServer:
1116
def __init__(self, desktop: "Sandbox") -> None:
@@ -288,6 +293,18 @@ def move_mouse(self, x: int, y: int):
288293
"""
289294
self.commands.run(f"xdotool mousemove --sync {x} {y}", envs={"DISPLAY": self._display})
290295

296+
def mouse_press(self, button: Literal["left", "right", "middle"] = "left"):
297+
"""
298+
Press the mouse button.
299+
"""
300+
self.commands.run(f"xdotool mousedown {MOUSE_BUTTONS[button]}", envs={"DISPLAY": self._display})
301+
302+
def mouse_release(self, button: Literal["left", "right", "middle"] = "left"):
303+
"""
304+
Release the mouse button.
305+
"""
306+
self.commands.run(f"xdotool mouseup {MOUSE_BUTTONS[button]}", envs={"DISPLAY": self._display})
307+
291308
def get_cursor_position(self) -> tuple[int, int]:
292309
"""
293310
Get the current cursor position.
@@ -365,7 +382,10 @@ def drag(self, fr: tuple[int, int], to: tuple[int, int]):
365382
:param from: The starting position.
366383
:param to: The ending position.
367384
"""
368-
self.commands.run(f"xdotool mousemove {fr[0]} {fr[1]} && xdotool mousedown 1 && xdotool mousemove {to[0]} {to[1]} && xdotool mouseup 1", envs={"DISPLAY": self._display})
385+
self.move_mouse(fr[0], fr[1])
386+
self.mouse_press()
387+
self.move_mouse(to[0], to[1])
388+
self.mouse_release()
369389

370390
def wait(self, ms: int):
371391
"""

0 commit comments

Comments
 (0)