Skip to content

Commit f6aebbd

Browse files
authored
Merge pull request #30 from e2b-dev/add-cua-methods
Add CUA methods
2 parents c566b62 + cc6aafa commit f6aebbd

File tree

6 files changed

+96
-8
lines changed

6 files changed

+96
-8
lines changed

.changeset/nice-clubs-scream.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+
openai cua support

README.md

+24-2
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ desktop.right_click()
175175
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
178+
desktop.drag((100, 100), (200, 200)) # Drag using the mouse
178179
```
179180

180181
**JavaScript**
@@ -190,6 +191,7 @@ await desktop.rightClick()
190191
await desktop.middleClick()
191192
await desktop.scroll(10) // Scroll by the amount. Positive for up, negative for down.
192193
await desktop.moveMouse(100, 200) // Move to x, y coordinates
194+
await desktop.drag([100, 100], [200, 200]) // Drag using the mouse
193195
```
194196

195197
### Keyboard control
@@ -208,7 +210,7 @@ desktop.write("Fast typing!", chunk_size=50, delay_in_ms=25) # Faster typing
208210
desktop.press("enter")
209211
desktop.press("space")
210212
desktop.press("backspace")
211-
desktop.press("ctrl+c")
213+
desktop.press(["ctrl", "c"]) # Key combination
212214
```
213215

214216
**JavaScript**
@@ -226,7 +228,7 @@ await desktop.write('Fast typing!', { chunkSize: 50, delayInMs: 25 }) // Faster
226228
await desktop.press('enter')
227229
await desktop.press('space')
228230
await desktop.press('backspace')
229-
await desktop.press('ctrl+c') // Copy
231+
await desktop.press(['ctrl', 'c']) // Key combination
230232
```
231233

232234
### Screenshot
@@ -305,6 +307,26 @@ const out = await desktop.commands.run('ls -la /home/user')
305307
console.log(out)
306308
```
307309

310+
### Wait
311+
312+
**Python**
313+
314+
```python
315+
from e2b_desktop import Sandbox
316+
desktop = Sandbox()
317+
318+
desktop.wait(1000) # Wait for 1 second
319+
```
320+
321+
**JavaScript**
322+
323+
```javascript
324+
import { Sandbox } from '@e2b/desktop'
325+
326+
const desktop = await Sandbox.create()
327+
await desktop.wait(1000) // Wait for 1 second
328+
```
329+
308330
## Under the hood
309331

310332
The desktop-like environment is based on Linux and [Xfce](https://www.xfce.org/) at the moment. We chose Xfce because it's a fast and lightweight environment that's also popular and actively supported. However, this Sandbox template is fully customizable and you can create your own desktop environment.

packages/js-sdk/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ await desktop.rightClick()
105105
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
108+
await desktop.drag([100, 100], [200, 200]) // Drag using the mouse
108109
```
109110

110111
### Keyboard control
@@ -122,7 +123,7 @@ await desktop.write('Fast typing!', { chunkSize: 50, delayInMs: 25 }) // Faster
122123
await desktop.press('enter')
123124
await desktop.press('space')
124125
await desktop.press('backspace')
125-
await desktop.press('ctrl+c') // Copy
126+
await desktop.press(['ctrl', 'c']) // Key combination
126127
```
127128

128129
### Screenshot
@@ -160,6 +161,15 @@ const out = await desktop.commands.run('ls -la /home/user')
160161
console.log(out)
161162
```
162163

164+
### Wait
165+
166+
```javascript
167+
import { Sandbox } from '@e2b/desktop'
168+
169+
const desktop = await Sandbox.create()
170+
await desktop.wait(1000) // Wait for 1 second
171+
```
172+
163173
## Under the hood
164174

165175
The desktop-like environment is based on Linux and [Xfce](https://www.xfce.org/) at the moment. We chose Xfce because it's a fast and lightweight environment that's also popular and actively supported. However, this Sandbox template is fully customizable and you can create your own desktop environment.

packages/js-sdk/src/sandbox.ts

+23-3
Original file line numberDiff line numberDiff line change
@@ -373,14 +373,34 @@ export class Sandbox extends SandboxBase {
373373

374374
/**
375375
* Press a key.
376-
* @param key - The key to press (e.g. "enter", "space", "backspace", etc.).
376+
* @param key - The key to press (e.g. "enter", "space", "backspace", etc.). Can be a single key or an array of keys.
377377
*/
378-
async press(key: string): Promise<void> {
378+
async press(key: string | string[]): Promise<void> {
379379
await this.commands.run(
380-
`xdotool key ${key}`, { envs: { DISPLAY: this.display } }
380+
`xdotool key ${Array.isArray(key) ? key.join('+') : key}`, { envs: { DISPLAY: this.display } }
381381
);
382382
}
383383

384+
/**
385+
* Drag the mouse from the given position to the given position.
386+
* @param from - The starting position.
387+
* @param to - The ending position.
388+
*/
389+
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+
);
394+
}
395+
396+
/**
397+
* Wait for the given amount of time.
398+
* @param ms - The amount of time to wait in milliseconds.
399+
*/
400+
async wait(ms: number): Promise<void> {
401+
await this.commands.run(`sleep ${ms / 1000}`, { envs: { DISPLAY: this.display } });
402+
}
403+
384404
/**
385405
* Open a file or a URL in the default application.
386406
* @param fileOrUrl - The file or URL to open.

packages/python-sdk/README.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ desktop.right_click()
102102
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
105+
desktop.drag((100, 100), (200, 200)) # Drag using the mouse
105106
```
106107

107108
### Keyboard control
@@ -118,7 +119,7 @@ desktop.write("Fast typing!", chunk_size=50, delay_in_ms=25) # Faster typing
118119
desktop.press("enter")
119120
desktop.press("space")
120121
desktop.press("backspace")
121-
desktop.press("ctrl+c")
122+
desktop.press(["ctrl", "c"]) # Key combination
122123
```
123124

124125
### Screenshot
@@ -156,6 +157,15 @@ out = desktop.commands.run("ls -la /home/user")
156157
print(out)
157158
```
158159

160+
### Wait
161+
162+
```python
163+
from e2b_desktop import Sandbox
164+
desktop = Sandbox()
165+
166+
desktop.wait(1000) # Wait for 1 second
167+
```
168+
159169
## Under the hood
160170

161171
The desktop-like environment is based on Linux and [Xfce](https://www.xfce.org/) at the moment. We chose Xfce because it's a fast and lightweight environment that's also popular and actively supported. However, this Sandbox template is fully customizable and you can create your own desktop environment.

packages/python-sdk/e2b_desktop/main.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -347,14 +347,34 @@ def break_into_chunks(text: str, n: int):
347347
f"xdotool type --delay {delay_in_ms} {quote_string(chunk)}", envs={"DISPLAY": self._display}
348348
)
349349

350-
def press(self, key: str):
350+
def press(self, key: str | list[str]):
351351
"""
352352
Press a key.
353353
354354
:param key: The key to press (e.g. "enter", "space", "backspace", etc.).
355355
"""
356+
if isinstance(key, list):
357+
key = "+".join(key)
358+
356359
self.commands.run(f"xdotool key {key}", envs={"DISPLAY": self._display})
357360

361+
def drag(self, fr: tuple[int, int], to: tuple[int, int]):
362+
"""
363+
Drag the mouse from the given position to the given position.
364+
365+
:param from: The starting position.
366+
:param to: The ending position.
367+
"""
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})
369+
370+
def wait(self, ms: int):
371+
"""
372+
Wait for the given amount of time.
373+
374+
:param ms: The amount of time to wait in milliseconds.
375+
"""
376+
self.commands.run(f"sleep {ms / 1000}", envs={"DISPLAY": self._display})
377+
358378
def open(self, file_or_url: str):
359379
"""
360380
Open a file or a URL in the default application.

0 commit comments

Comments
 (0)