Xdotool is a versatile command line utility that allows simulating mouse and keyboard inputs in Linux. With xdotool, you can automate repetitive tasks, create keyboard shortcuts to complex commands, and script advanced workflows. This comprehensive guide covers various methods and examples to control mouse clicks, scroll wheel, key presses, key sequences, modifier keys and more using xdotool.

Installing Xdotool

Xdotool is included in the repositories of most popular Linux distributions like Ubuntu, Fedora, Arch Linux etc. To install it on Ubuntu or Debian, run:

sudo apt update
sudo apt install xdotool

On Fedora:

sudo dnf install xdotool

And on Arch Linux:

sudo pacman -S xdotool

Simulate Mouse Clicks

To simulate a left mouse click at the current pointer location, use:

xdotool click 1

The numbers denote the following mouse buttons:

  • 1 – Left mouse button
  • 2 – Middle button
  • 3 – Right button
  • 4 – Scroll up
  • 5 – Scroll down

You can also specify x and y coordinates to simulate click at a specific position on the screen:

xdotool mousemove 100 200 click 1

This will move mouse pointer to 100px from left and 200px from top edge before clicking.

To double click or triple click, use:

xdotool click --repeat 3 1

Similarly, you can create click and drag effect with press, move and release commands:

xdotool mousedown 1
xdotool mousemove 100 200 
xdotool mouseup 1

Key Press and Release

To simulate pressing a key, use:

xdotool key a

This will press and release the ‘a‘ key. To separate press and release events:

xdotool keydown a
xdotool keyup a 

You can also simulate multiple keys in sequence:

xdotool key HelloWorld

This will type out the text "HelloWorld".

Repeat Key Presses

To automate multiple repeats of a key, use the --repeat parameter:

xdotool key --repeat 10 a

The above command will press ‘a‘ 10 times with a short delay between each press.

You can also add custom delay between keys with --delay option:

xdotool key --repeat 5 --delay 500 a 

Here ‘a‘ will be pressed 5 times with a 500ms delay between each keystroke.

Modifier Keys

Modifier keys like Control, Alt, Shift etc can be combined with other keys using ‘+‘ sign. For example:

xdotool key ctrl+c

This will simulate the key combination Ctrl + C to copy text. Other examples:

xdotool key alt+F4   # Alt + F4 to close window
xdotool key shift+a # Shift + A 

Key Sequences

You can create a sequence of keys to be pressed with custom delays as shown below:

DELAY=500
xdotool type --delay $DELAY "First line"
xdotool key KP_Enter  # Enter key
xdotool type --delay $DELAY "Second line"
xdotool key KP_Enter

This script will type the text in two separate lines with a delay of 500ms between keystrokes.

Automate Window Actions

Xdotool can also get information and control aspects of the active window focus. For example, to get the ID of currently active window and minimize it:

ACTIVE_WND=$(xdotool getactivewindow)
xdotool windowminimize $ACTIVE_WND

Some other window actions include close, maximize, restore, resize, move and more. Refer to the xdotool manual for additional details.

Map to Keyboard Shortcuts

For quick access, you can assign xdotool scripts and commands to custom keyboard shortcuts:

  1. Go to Settings > Keyboard Shortcuts
  2. Click the ‘+‘ icon and assign name and command

For example, create a custom shortcut as:

  • Name: Mute Volume
  • Command: xdotool key XF86AudioMute

Now just press this keyboard shortcut anytime to mute/unmute volume.

For multi-step procedures, save the commands in an executable script file like script.sh and map this script to the shortcut instead.

Conclusion

Xdotool is a feature-rich utility that opens up endless possibilities for test automation, reducing repetitive tasks, creating workflows and customizing Linux desktop environment. It can simulate all kinds of mouse, keyboard and window actions based on user requirements. Refer to xdotool man pages or source code for additional capabilities.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *