Mouse and keyboard input for desktop agents: xdotool, pyautogui and ydotool
The gist
On an X11 desktop, send an agent's actions with xdotool (mousemove, click, type, key) or pyautogui from Python; both inject events through the X server. On Wayland, use a tool below the display server such as ydotool. Wait for the screen to settle before the next screenshot.
Words: VMForAI editors / Out / Fixed up / 4 min
An agent's actions reach the desktop through an input tool that turns "click at (612, 344)" into real pointer and keyboard events. On an X11 desktop, xdotool from the shell and pyautogui from Python are the usual choices. The tool matters less than three habits: one command per action, the right coordinate space, and a pause before the next screenshot.
How do I send a click and a key press with xdotool?
Send a click with mousemove then click, and keys with type for text and key for shortcuts. These commands assume DISPLAY points at the agent's display:
export DISPLAY=:99
xdotool mousemove --sync 612 344 click 1 # move, wait until moved, left click
xdotool click --repeat 2 1 # double click
xdotool type --delay 30 'quarterly report' # type text, 30 ms between keys
xdotool key ctrl+s # keyboard shortcut
xdotool key Return
From the xdotool manual (read 2026-11-26):
Table: xdotool commands an agent harness uses
| Command | What the manual says | Agent note |
|---|---|---|
mousemove X Y | "Move the mouse to the specific X and Y coordinates on the screen." | Add --sync to "wait until the mouse is actually moved" |
click BUTTON | "Left mouse is 1, middle is 2, right is 3, wheel up is 4, wheel down is 5." | --repeat 2 for a double click |
mousedown / mouseup | Only a press or only a release | Build drags: mousedown, mousemove, mouseup |
type TEXT | "Delay between keystrokes. Default is 12ms." | Raise --delay if characters go missing |
key KEYS | Same 12 ms default delay | Use for shortcuts such as ctrl+s |
getmouselocation | "Outputs the x, y, screen, and window id of the mouse cursor." | Log it after each action |
search / windowactivate | Find windows by name or class, then activate | Useful in setup scripts, not as agent actions |
Source: xdotool manual, Debian bookworm, read 2026-11-26.
The manual explains how it works: "It does this using X11's XTEST extension and other Xlib functions", and warns "If your X server does not support XTEST, then some typing and mouse movement features may not work." Check for XTEST in your desktop's start-up test.
How do I map model actions to input commands?
Map each kind of model action to one input call, and keep the table in your harness, not in the prompt.
Table: Example mapping from agent actions to xdotool (an example)
| Model action | xdotool command |
|---|---|
| click(x, y) | xdotool mousemove --sync x y click 1 |
| double_click(x, y) | xdotool mousemove --sync x y click --repeat 2 1 |
| right_click(x, y) | xdotool mousemove --sync x y click 3 |
| drag(x1, y1, x2, y2) | xdotool mousemove --sync x1 y1 mousedown 1 mousemove --sync x2 y2 mouseup 1 |
| scroll_down(n) | xdotool click --repeat n 5 |
| type(text) | xdotool type --delay 30 "text" |
| key(combo) | xdotool key combo |
Before sending, convert the model's coordinates from the screenshot it saw to the real screen, as described in screen resolution and DPI for computer-use agents. Pass text as a separate argument, never through a shell string the model wrote, so a model cannot inject shell commands through "text".
When should I use pyautogui instead?
Use pyautogui when the harness is in Python or must also run on Windows or macOS. Its documentation says "PyAutoGUI lets your Python scripts control the mouse and keyboard to automate interactions with other applications" and that it "works on Windows, macOS, and Linux" (PyAutoGUI documentation, read 2026-11-26). It is also the action language of OSWorld, whose actions are Python calls to pyautogui (OSWorld paper, section 2.4, read 2026-11-26).
Two defaults change agent behaviour (PyAutoGUI documentation):
- Fail-safe. If the mouse is "in any of the four corners of the primary monitor" when a function is called, pyautogui raises
pyautogui.FailSafeException. The project strongly advises against turning it off; if an agent must work near corners, handle the exception in the harness. - Pause. Every call is followed by a delay "set by the
pyautogui.PAUSEsetting, which is0.1by default."
pyautogui works only with the primary monitor, so give agent desktops a single screen.
What about Wayland desktops?
On a Wayland session, use a tool that works below the display server, because xdotool reaches only the X server. ydotool "uses the kernel's uinput framework to emulate an input device" and is "not limited to Wayland". Since version 1.0.0 its background daemon ydotoold must be running, and it needs access to /dev/uinput, "which usually requires root privileges" (ydotool repository, read 2026-11-26).
Its moves are relative unless asked otherwise: ydotool mousemove --absolute -x 100 -y 100 moves to a screen position. Its key command takes raw key codes, not names. For most agent desktops, the simpler path is an X11 session, which is also what OSWorld's in-VM server setup asks for: it says to switch from Wayland to Xorg (OSWorld server README, read 2026-11-26).
How long should the harness wait after an action?
Wait until the screen has settled, not a fixed time you hope is enough. Applications animate, load and redraw after a click; a screenshot taken too early shows the old state and the model repeats the action. OSWorld's environment waits about two seconds after each action before it takes the observation: its step function has pause=2 and calls time.sleep(pause) (OSWorld desktop_env.py, read 2026-11-26). A tighter approach is to take screenshots in a short loop and continue when two in a row are the same, with a time limit.
What we read for this article
- xdotool manual, Debian bookworm, read 2026-11-26
- PyAutoGUI documentation, read 2026-11-26
- ydotool repository, read 2026-11-26
- OSWorld paper (arXiv 2404.07972), read 2026-11-26
- OSWorld server README, read 2026-11-26
- OSWorld desktop_env.py, read 2026-11-26
Asked
How does xdotool send input?
Its manual says it uses X11's XTEST extension and other Xlib functions. If the X server does not support XTEST, some typing and mouse movement features may not work, so check for the extension when the desktop starts.
How fast does xdotool type?
The default delay between keystrokes is 12 milliseconds for both type and key. Raise it with --delay if an application drops characters.
Does xdotool work on Wayland?
xdotool talks to the X server, so on a Wayland session it reaches only X applications. ydotool works through the kernel's uinput framework instead, and is not limited to Wayland.
Why does pyautogui raise FailSafeException?
Its fail-safe is on by default. If the mouse is in any of the four corners of the primary monitor when a pyautogui function is called, it raises pyautogui.FailSafeException. Agents that click near corners need to know this.