tutorials / linux-input-automation

Linux Input Automation

Einleitung

Automatisierung von Maus- und Tastatureingaben ist nützlich für Testing, Scripting und Workflow-Automatisierung. Dieser Guide zeigt die wichtigsten Tools.

Zwischenablage

In Zwischenablage kopieren

xclip -sel clip [file]

# Pipe-Output
uname -a | xclip -sel clip

Aus Zwischenablage lesen

xclip -o -sel clip

Wayland-Alternative:

wl-copy < [file]
wl-paste

Maus-Steuerung

Maus absolut bewegen

xdotool mousemove [x] [y]

ydotool mousemove --absolute [x] [y]

Beispiel: Maus zu Position (500, 300) bewegen:

xdotool mousemove 500 300

Maus relativ bewegen

xdotool mousemove_relative [x] [y]

ydotool mousemove [x] [y]

Beispiel: 100 Pixel nach rechts, 50 nach unten:

xdotool mousemove_relative 100 50

Mausklick

xdotool click 1  # 1=links, 2=mitte, 3=rechts

ydotool click left  # left, right, middle

Doppelklick

xdotool click --repeat 2 1

Rechtsklick

xdotool click 3

Maus-Position abfragen

xdotool getmouselocation

Tastatur-Steuerung

Text tippen

xdotool type "Hello world"

ydotool type "Hello world"

wtype "Hello world"

Text mit Verzögerung tippen (simuliert Tippen)

xdotool type --delay 100 "Hello world"

Tastendruck

xdotool key Escape

ydotool key Escape

wtype -k Escape

Tastenkombinationen

# Ctrl+C
xdotool key ctrl+c

# Ctrl+Alt+Delete
xdotool key ctrl+alt+Delete

# Alt+Tab
xdotool key alt+Tab

Mehrere Tasten nacheinander

xdotool key ctrl+a ctrl+c

Taste gedrückt halten

xdotool keydown shift
xdotool key h e l l o
xdotool keyup shift

Fenster-Steuerung

Aktives Fenster ermitteln

xdotool getactivewindow

Fenster nach Name finden

xdotool search --name "Firefox"

Fenster fokussieren

xdotool windowactivate [window_id]

Fenster verschieben

xdotool windowmove [window_id] [x] [y]

Fenster-Größe ändern

xdotool windowsize [window_id] [width] [height]

Praktische Automation-Beispiele

Auto-Clicker

#!/bin/bash
while true; do
    xdotool click 1
    sleep 0.5
done

Formular automatisch ausfüllen

#!/bin/bash
# Tab zur nächsten Eingabe
xdotool key Tab
sleep 0.2

# Name eingeben
xdotool type "Max Mustermann"
xdotool key Tab
sleep 0.2

# Email eingeben
xdotool type "max@example.com"
xdotool key Tab
sleep 0.2

# Submit
xdotool key Return

Screenshot-Automation

#!/bin/bash
for i in {1..5}; do
    # Screenshot
    gnome-screenshot -f screenshot_$i.png

    # Nächstes Fenster
    xdotool key alt+Tab
    sleep 1
done

Text in alle offenen Fenster eingeben

#!/bin/bash
text="Broadcast message"

for window in $(xdotool search --onlyvisible --class ""); do
    xdotool windowactivate $window
    sleep 0.2
    xdotool type "$text"
    xdotool key Return
done

X11 vs Wayland

X11 (traditionell)

  • xdotool: Volle Funktionalität
  • xclip: Zwischenablage
  • Funktioniert mit allen X11-Anwendungen

Wayland (modern)

  • ydotool: Alternative zu xdotool (benötigt root/setup)
  • wtype: Tastatureingaben
  • wl-clipboard: Zwischenablage (wl-copy, wl-paste)

Wayland Setup für ydotool

# ydotool installieren
sudo apt install ydotool

# Daemon starten
sudo ydotoold &

# Nutzen
ydotool type "Hello Wayland"

Debugging & Testing

Keycode herausfinden

xev

Drücke Tasten im erscheinenden Fenster, um Keycodes zu sehen.

Maus-Events beobachten

xev | grep button

Sicherheitshinweise

  • Vorsicht bei Root-Rechten: ydotool braucht oft elevated permissions
  • Teste in sicherer Umgebung: Automation kann unerwartete Aktionen ausführen
  • Verzögerungen einbauen: Mit sleep zwischen Aktionen
  • Escape-Mechanismus: Immer Möglichkeit zum Abbrechen vorsehen

Best Practices

  • Nutze xdotool unter X11: Stabil und gut dokumentiert
  • Teste kleine Schritte: Vor komplexen Automationen
  • Error-Handling: Prüfe ob Befehle erfolgreich waren
  • Verzögerungen: Gib Anwendungen Zeit zu reagieren
  • Dokumentiere Automationen: Für spätere Wartung

Installation

# X11 Tools
sudo apt install xdotool xclip

# Wayland Tools
sudo apt install ydotool wl-clipboard wtype

Weitere Ressourcen