Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: March 18, 2024
We can automate mouse movements using the command line by utilizing tools that simulate mouse actions. One such tool is xdotool, which is commonly available in many Linux distros.
In this tutorial, we’ll show how to use xdotool to automate mouse movements using the command line.
If xdotool isn’t installed on our Linux system, we can typically install it using our package manager. For example, on Ubuntu or Debian-based system, we can use the following command:
$ sudo apt install xdotool
This will install xdotool and its dependencies.
In this section, we’ll explore the basic command usage of xdotool.
To use xdotool to move the mouse cursor to a specific position on the screen, we can use the mousemove command. So let’s move the cursor to the top-left corner:
$ xdotool mousemove 0 0
This’ll move the cursor to the coordinates (0,0). Hence, we can specify any coordinates we want if they’re within the screen resolution.
We can determine the coordinates to use by running xdotool getmouselocation command when we move the cursor to any point on the screen.
We can use the xdotool click command to click the mouse button. So let’s click the left mouse button:
$ xdotool click 1
This will simulate a single click of the left mouse button. We can also specify other buttons, such as 2 for the middle button or 3 for the right button.
Let’s explore some advanced commands available in xdotool.
To use xdotool to perform complex mouse actions, such as drag and drop, scroll, or double click, we can use the mousedown, mouseup, and mousedrag commands. Let’s drag a file from one folder to another:
$ xdotool mousemove 100 100 mousedown 1 mousemove 200 200 mouseup 1
This’ll move the cursor to the coordinates (100,100), press and hold the left mouse button, move the cursor to the coordinates (200, 200), and release the left mouse button.
We can use xdotool to scroll up or down with the mouse wheel:
$ xdotool click --repeat 10 --delay 50 4
This will simulate clicking the mouse wheel up 10 times with a delay of 50 milliseconds between each click. We can also use 5 instead of 4 for scrolling down.
Let’s also use xdotool to double-click the left mouse button:
$ xdotool click --repeat 2 --delay 100 1
This’ll simulate clicking the left mouse button twice with a delay of 100 milliseconds between each click.
We can use the sleep command to add a delay between commands. For example, let’s add a one-second delay between two commands:
$ xdotool mousemove 100 100 sleep 1 click 1 mousemove 200 200
This’ll move the cursor to the coordinates (100, 100) and wait for a second. Then, it’ll perform a left click and then move to the coordinates (200, 200).
In this tutorial, we’ve shown how we use xdotool to automate mouse movements using the command line. Then, we showed how to know the coordinate to use by using the getmouselocation command. Also, we explore some basic and advanced usage of the command and show examples that could guide us to using the xdotool for automating mouse movements.