Skip to content

About

Fork -- OTAKUWeBer--Wayland-automation -- Python Library independently maintained

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

 
 

Repository files navigation

personal fork of OTAKUWeBer's wayland-automation

"Move the cursor without the typical, manditory click!"

area what this fork does
cursor movement click(x, y) moves the cursor, and clicking is just a matter of passing a button along with it. Left, right and middle buttons all support press/release holds, so multi-segment drags work too
scroll wheel scroll(1) keeps the fork's one-step-down behavior; scroll(clicks=1) uses upstream's one-step-up convention. Both axes and scrolling at a point are supported
physical keyboard named keys go through the seat's XKB map with physical keycodes, and modifiers keep their left/right identity. Print, letters and function keys fire compositor shortcuts and key displays properly, and Unicode text goes through wtype for characters the layout doesn't have
timed input Keyboard.sequence() fires key events in order, at millisecond granularity, on a single connection. Chords keep their held keys between calls, and closing or cancelling releases the keyboard's keys and connection
pointer lifecycle ordinary top-level calls release their virtual pointer when done; only explicit button holds share a pointer across calls. Sequential object IDs, full socket reads and bounded waits keep pointer requests in step with the Wayland protocol
desktop geometry desktop bounds account for monitor origins, scaling and rotation, including negative coordinates and multi-screen setups. Hyprland and Sway report their logical geometry directly
cursor backends Hyprland, wl-find-cursor, XWayland, plus optional KDE/evdev tracking. The CLI can also take position snapshots and check dependencies, and the optional packages come in via the kde and evdev extras
pip install "git+https://github.com/CommanderTurtle/Wayland-automation.git"

OTAKUWeBer/Wayland-automation - visit for the upstream repo!

Upstream notes (helpful to agents):

click to expand

Wayland Automation

PyPI version Python 3.6+ License: MIT

A Python library for mouse and keyboard automation on Wayland compositors. Works out of the box on Hyprland, Sway, and other wlroots-based compositors.

Features

  • Mouse — move, click, hold and release buttons, swipe (drag), auto-click, and scroll at the cursor or at a screen coordinate
  • Keyboard — type text, press individual keys, and trigger hotkeys (e.g. Ctrl+S)
  • Cursor Tracking — real-time cursor position via multiple backends (Hyprland → wl-find-cursor → xdotool → evdev)
  • Convenience API — top-level functions so you can import wayland_automation as wa and go
  • Resilient — automatic reconnection logic for Wayland socket resets

Installation

1. Install the package

pip install wayland-automation

2. Install system dependencies

Dependency What it does Install command
libxkbcommon Physical keyboard codes and the existing seat layout sudo pacman -S libxkbcommon (Arch) / sudo apt install libxkbcommon0 (Debian) / sudo dnf install libxkbcommon (Fedora)
wtype Unicode text fallback for characters absent from the active layout sudo pacman -S wtype (Arch) / sudo apt install wtype (Debian) / sudo dnf install wtype (Fedora)
wayland-utils Screen resolution detection sudo pacman -S wayland-utils (Arch) / sudo apt install wayland-utils (Debian) / sudo dnf install wayland-utils (Fedora)

Mouse-tracking backends (pick the one for your compositor)

The library auto-detects the best backend. You only need to install the one that matches your setup:

Backend Compositor Install
hyprctl Hyprland Pre-installed with Hyprland
wl-find-cursor Sway / wlroots See build from source below
xdotool XWayland apps sudo pacman -S xdotool / sudo apt install xdotool
evdev (fallback) Any pip install evdev + add user to input group (see below)
Building wl-find-cursor from source
git clone https://github.com/cjacker/wl-find-cursor.git
cd wl-find-cursor
make
sudo cp wl-find-cursor /usr/local/bin/
Setting up evdev fallback
sudo usermod -aG input $USER
# Log out and back in for the group change to take effect

Quick Start

Top-level convenience API

The simplest way to use the library — no class instantiation needed:

import wayland_automation as wa

# Mouse
wa.click(250, 300, "left")                # left-click at (250, 300)
wa.click(400, 500, "right")               # right-click
wa.scroll(1)                              # down one wheel step at the cursor
wa.scroll(vertical=-1, horizontal=2)      # up one step and right two
wa.scroll(clicks=1)                       # upstream convention: up one step
wa.scroll(1, 400, 500)                    # up one step at (400, 500)
wa.mouse_down(100, 200, "left")           # start a drag
wa.mouse_up(350, 200, "left")             # move there, then release
wa.swipe(100, 200, 800, 200)              # drag from point A to B
wa.auto_click(initial_delay=2, interval=0.1, duration=5)  # auto-clicker

# Keyboard
wa.typewrite("Hello Wayland!", interval=0.05)  # type text character by character
wa.press("enter")                              # press a single key
wa.hotkey("ctrl", "s")                         # key combination

Using the classes directly

For more control, instantiate Mouse or Keyboard:

from wayland_automation import Mouse, Keyboard

mouse = Mouse()
mouse.click(250, 300, "left")       # left-click at (250, 300)
mouse.click(600, 400, "nothing")    # move cursor without clicking
mouse.swipe(0, 500, 1000, 500, speed=0.5)  # fast swipe (0.5 s)

kb = Keyboard()
kb.typewrite("Hello!", interval=0.05)
kb.press("enter")
kb.hotkey("ctrl", "a")              # select all
kb.keyDown("shift")                 # hold shift
kb.keyUp("shift")                   # release shift
kb.close()                          # also releases any remaining owned holds

Named keys and timed sequences use zwp_virtual_keyboard_manager_v1 with the seat's existing XKB keymap and matching physical evdev codes. Print is code 99, not wtype's private code 1 (which physical-key observers and normal Hyprland shortcut matching interpret as Escape). Modifiers emit actual key edges as well as their XKB state. No global/per-device config changes, runtime compositor Lua, uinput permissions, or pointer backend changes are required.

One connection is kept for each chord/sequence and for explicit keyDown()/keyUp() holds. Independent operations reread the seat map; use with Keyboard() as kb: or close() to balance holds on cancellation. Hyprland and Sway active-group discovery is read-only. Setup/keymap errors are reported, never silently retried as wtype key commands.

typewrite() uses physical keys when the whole string is representable in the active layout, including Shift/AltGr levels. Otherwise the original checked wtype Unicode text path is used before any prefix is typed. That fallback still uses wtype's private symbol codes and is not a physical-key HUD contract; it is never used for named keys, chords, or timed key sequences. A private seat map left active by another virtual keyboard is rejected rather than guessed.

The offline regression bench checks actual wire keycodes and lifecycle cleanup; on Linux it also checks real libxkbcommon US/French maps and lock-state handling:

python -m unittest discover -s tests -v

Live verification should compare Print, q, a, F13, left/right modifiers and an overlapping chord in the HUD and a dedicated key-event viewer, then check the configured screenshot shortcut. Do not reload the compositor or add observers for this check.

Cursor Tracking

Stream real-time cursor coordinates:

from wayland_automation import mouse_position_generator

for x, y in mouse_position_generator(interval=0.1):
    print(f"Cursor at: ({x}, {y})")
    # break whenever you want to stop

CLI Usage

After installing the package, a wayland-automation command is available:

wayland-automation                     # print available commands
wayland-automation scroll 1            # up one step at the current pointer
wayland-automation scroll -2 500 300   # down two steps at (500, 300)

You can also run the individual modules directly:

# Mouse — click at (x, y)
python -m wayland_automation.mouse_controller click 250 300 left

# Mouse — swipe
python -m wayland_automation.mouse_controller swipe 100 200 800 200

# Mouse — auto-click (delay interval duration button)
python -m wayland_automation.mouse_controller autoclick 3 0.1 10 left

# Mouse — run built-in test
python -m wayland_automation.mouse_controller test

# Cursor position watcher
python -m wayland_automation.mouse_position

API Reference

wayland_automation.click(x, y, button=None)

Move the pointer to (x, y). Pass "left", "right", or "middle" to click; omit the button or use "nothing" to move only.

wayland_automation.scroll(...)

Choose the spelling that matches your wheel convention. These forms also work on a Mouse instance.

Call Direction
scroll(1) or scroll(vertical=1) Down one step at the cursor (fork convention).
scroll(vertical=-1, horizontal=2) Up one step and right two at the cursor.
scroll(clicks=1) Up one step at the cursor (upstream convention).
scroll(1, x, y) or scroll(clicks=1, x=x, y=y) Move to (x, y), then scroll up one step. Supply both coordinates.

The CLI uses the upstream convention: wayland-automation scroll CLICKS [X Y] scrolls up for a positive count and down for a negative count.

wayland_automation.mouse_down(...) / mouse_up(...)

Hold or release a button at the current cursor (e.g. mouse_down("middle")) or at a point (e.g. mouse_up(350, 200, "middle")). The default button is left. The aliases mouseDown and mouseUp work both at module level and on Mouse. Top-level holds share a pointer; ordinary click, swipe, auto_click, and scroll calls each use a scoped pointer. For a drag with intermediate moves, use one Mouse:

with wa.Mouse() as mouse:
    mouse.mouse_down(100, 200, "left")
    mouse.click(200, 250)                 # move only, on the held pointer
    mouse.mouse_up(350, 200, "left")

Closing the Mouse releases its held buttons. For top-level holds, call wa.cleanup() if interrupted; it also runs on normal interpreter exit.

wayland_automation.swipe(start_x, start_y, end_x, end_y, speed="normal")

Drag from one point to another. speed is duration in seconds ("normal" = 1.0 s).

wayland_automation.auto_click(initial_delay, interval, duration, button)

Wait initial_delay seconds, then click every interval seconds for duration seconds.

wayland_automation.typewrite(text, interval=0)

Type text one character at a time with an optional delay between characters.

wayland_automation.press(key)

Press and release a single key (e.g. "enter", "tab", "backspace", "space").

wayland_automation.hotkey(*keys)

Press a key combination (e.g. hotkey("ctrl", "shift", "t")). Modifier keys: ctrl, alt, shift, super.

wayland_automation.mouse_position_generator(interval=0.2, print_output=False)

Generator yielding (x, y) tuples of the current cursor position.


Compositor Compatibility

Compositor Mouse Keyboard Cursor Tracking Notes
Hyprland ✅ ✅ ✅ hyprctl Full support (wlroots-based)
Sway ✅ ✅ ✅ wl-find-cursor Full support (wlroots-based)
KDE Plasma ❌ ✅ via wtype ❌ Mouse planned for KDE 6.5 (pointer-warp-v1)
GNOME ❌ ✅ via wtype ❌ Requires future protocol support

Troubleshooting

Problem Likely cause Fix
BrokenPipeError / SIGPIPE Compositor disconnected the client Ensure you are on a supported compositor and the protocol is available
WaylandProtocolError zwlr_virtual_pointer_manager_v1 not supported Switch to a wlroots-based compositor (Hyprland / Sway)
wtype not found System dependency missing Install wtype (see Installation)
Cursor tracking returns nothing No backend available Install the backend matching your compositor (see table above)

Architecture

wayland_automation/
├── __init__.py             # Public API & convenience functions
├── __main__.py             # CLI entry point, dependency checks
├── mouse_controller.py     # Wayland socket communication for virtual pointer
├── keyboard_controller.py  # Keyboard automation via wtype
├── mouse_position.py       # Multi-backend cursor position tracking
└── utils/
    └── screen_resolution.py  # Screen resolution detection

Roadmap

  • KDE Plasma 6.5+ pointer-warp-v1 protocol support
  • Improved error messages for unsupported compositors
  • Scroll wheel support

Contributing

Contributions welcome! See CONTRIBUTING.md for guidelines.

Timed input and cleanup

Keyboard.sequence([(0, "down", "ctrl"), (0, "down", "c"), (100, "up", "c"), (100, "up", "ctrl")]) runs the complete sequence on one virtual keyboard connection. Separate keyDown/keyUp calls on the same keyboard can retain a held key. Input failures are raised to the caller. Mouse can be used as a context manager to release its virtual pointer deterministically. mouse.scroll(vertical=1, horizontal=0) scrolls one step downward; negative steps scroll upward/leftward. Run the device-free regressions with python -m unittest discover -s tests.

Mouse.button("left", True) and Mouse.button("left", False) hold/release a button on the same pointer, allowing multi-segment drags. The optional evdev tracking fallback is available through pip install 'wayland-automation[evdev]'; Hyprland tracking does not require input-group access.

About

Fork -- OTAKUWeBer--Wayland-automation -- Python Library independently maintained

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages