Installing Xdotool
To install xdotool in Ubuntu, run the command below:
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.
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 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:
You can find correct names for keyboard keys by using the following command:
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:
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.
You can also use a while loop to repeatedly simulate keypresses until the loop is manually interrupted using <CTRL+C> key.
Simulate a Key Sequence
To simulate multiple keys one after another, use a command in the following format:
Simulate Mouse Clicks
To simulate a right click at current location of pointer, run the command below:
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:
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:
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:
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.