1. Overview

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.

2. Installation

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.

3. Basic Usage of xdotool

In this section, we’ll explore the basic command usage of xdotool.

3.1. mousemove Command

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.

3.2. click Command

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.

4. Advanced Usage of xdotool

Let’s explore some advanced commands available in xdotool.

4.1. mousedownmouseup, and mousedrag Commands

To use xdotool to perform complex mouse actions, such as drag and drop, scroll, or double click, we can use the mousedownmouseup, 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.

4.2. Scrolling and Double Clicking Simulation with xdotool

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.

4.3. Adding a Delay Between Commands

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).

5. Conclusion

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.

Comments are closed on this article!