Open Source

How to Use Xdotool to Stimulate Mouse Clicks and Keystrokes in Linux

Xdotool is a free and open source command line tool for simulating mouse clicks and keystrokes. This article will cover a brief guide on using xdotool to automate keyboard and mouse inputs. Xdotool works on X display server only, and there is no support for Wayland (at the time of writing this article).

Installing Xdotool

To install xdotool in Ubuntu, run the command below:

$ sudo apt install xdotool

To install xdotool in other Linux distributions, search for the term “xdotool” in the package manager. Xdotool is included in repositories of most Linux distributions.

Simulate a Keystroke

You can use xdotool to simulate a keystroke by running the command below. Replace “n” with any valid key or character in keyboard layout.

$ xdotool key n

Note that the command above will simulate both a key press and a key release one after another and you won’t notice any delay between these events. In case you want to simulate them separately, use the following commands:

$ xdotool keydown n
$ xdotool keyup n

Simulate a Keystroke with a Modifier Key

Modifier keys modify behavior of keypresses when two or more keys are pressed simultaneously. E.g. <CTRL+S>, <SHIFT+1> and so on. The process for executing key combinations using xdotool is also pretty similar:

$ xdotool key ctrl+s

You can find correct names for keyboard keys by using the following command:

$ xev

A small window will open. Keep it focused, then press any key. You will get the name of the key in terminal output.

Simulate Repeat Keys / Turbo / Rapid Fire

To simulate multiple keypresses over a period of time, you will have to specify how many number of times keypresses have to be simulated and delay between each key press. To do so, run a command in the following format:

$ xdotool key --repeat 5 --delay 50 n

Replace values of “–repeat” and “–delay” (in milliseconds) switches according to your requirements. Note that I had repetition issues when I set the delay to be more than 500 ms. This could be a bug or intended behavior and if you are facing similar issues, avoid setting high delay values. You can use “for” and “while” loop statements to overcome this limitation.

The command stated below will input the “n” key three times with a delay of 2 seconds in between each keystroke.

$ for i in {1..3}; do xdotool key n; sleep 2; done

You can also use a while loop to repeatedly simulate keypresses until the loop is manually interrupted using <CTRL+C> key.

$ while true; do xdotool key n; sleep 2; done

Simulate a Key Sequence

To simulate multiple keys one after another, use a command in the following format:

$ xdotool key x y z

Simulate Mouse Clicks

To simulate a right click at current location of pointer, run the command below:

$ xdotool click 3

Replace “3” with with any number from the reference below:

  • 1 – Left click
  • 2 – Middle click
  • 3 – Right click
  • 4 – Scroll wheel up
  • 5 – Scroll wheel down

If you want to use a different set of coordinates, use a command in the following format:

$ xdotool mousemove 100 100 click 3

Replace “100” with your desired coordinates as “X” and “Y” from the top left corner of the screen.

Note that various examples explained so far with keystrokes can also be used with mouse clicks.

Get Active Window and Minimize It

The following command will get ID of window currently in focus and then minimize it:

$ xdotool getactivewindow windowminimize

Refer to the Man Page

Xdotool includes numerous options and you can use countless different combinations to customize behaviour of keystrokes and mouse clicks. It is not possible to cover all use cases here, you can refer to the man page by running the command below:

$ man xdotool

You can also access the man page online.

Map Xdotool Commands and Scripts to Keyboard Shortcuts

You can use keyboard shortcuts configuration GUI available in system settings of your distribution to assign simple xdotool commands to custom keyboard shortcuts. In case of complex and multiple statements, save the commands in a script and then map the script to keyboard shortcut.

Conclusion

Xdotool is one of the best utilities available for automating keyboard and mouse inputs in Linux. You can run simple macros as well as complex commands by chaining multiple simulated inputs.

About the author

Nitesh Kumar

Nitesh Kumar

I am a freelancer software developer and content writer who loves Linux, open source software and the free software community.