Source: ydotool — Generic Command-Line Automation Tool

Overview

ydotool is a Wayland-compatible mouse and keyboard automation tool (unlike xdotool which requires X11). Works on X11, Wayland, text console, fbdev apps — anything that accepts input.

Uses the Linux kernel’s uinput framework to emulate a virtual input device.

Setup (install from source)

sudo apt update
sudo apt install git cmake scdoc pkg-config
git clone https://github.com/ReimuNotMoe/ydotool.git
cd ydotool
mkdir build && cd build
cmake ..
time make -j "$(nproc)"
sudo make install

Required: ydotoold daemon

ydotool requires ydotoold (the background daemon) to be running first. Without it, nothing works.

# Start daemon (background, disowned so it survives terminal close)
sudo ydotoold & disown
# press Enter a couple times
# keep this terminal window open

Set the socket path (add to ~/.bashrc):

export YDOTOOL_SOCKET=/tmp/.ydotool_socket

Source it: source ~/.bashrc

Optional: allow use without sudo:

sudo chmod 777 /tmp/.ydotool_socket

Commands

Mouse movement

# Relative move (-100 in x, +100 in y)
ydotool mousemove -x -100 -y 100
 
# Absolute move to (100, 100)
ydotool mousemove --absolute -x 100 -y 100

Mouse clicks

ydotool click 0xC0             # left click
ydotool click 0xC1             # right click
ydotool click --repeat 5 --next-delay 25 0xC0   # 5 left clicks, 25ms apart

Keyboard

# Key press/release with keycodes (format: keycode:1=press, keycode:0=release)
ydotool key 29:1 56:1 59:1 59:0 56:0 29:0   # Ctrl+Alt+F1
 
# Type a string
ydotool type 'hello world'

Key codes are from /usr/include/linux/input-event-codes.h.

Calling from Python (subprocess)

import subprocess
 
# Move mouse relatively
subprocess.run(['ydotool', 'mousemove', '-x', str(dx), '-y', str(dy)])
 
# Left click
subprocess.run(['ydotool', 'click', '0xC0'])

Why ydotoold is required

When ydotool creates a virtual input device, there’s a delay before the OS recognizes it. ydotoold holds a persistent virtual device open, so ydotool just sends commands to it instead of creating a new device each time.

See Also