How to simulate mouse movements using Python?


When it comes to automation, be it to setup your computer on start-up or to farm coins on a clicker game, it becomes essential to simulate mouse movements and clicks. And what better way to do this than use Python!

For performing this particular task of automating or simulating your mouse movement, we will be using Python’s mouse library that has various methods and functionalities to help simulate your mouse on your computer.

Now that you know what we will be working with, let us get started!

Getting Started

Firstly, we need to install the mouse library. Since this library does not come pre-packaged with Python, we have to download and install it using the pip package manager.

To do this, launch your terminal and type the following "pip" command −

pip install mouse

Using this module, we will be able to hook global events, register hotkeys, simulate mouse movements, clicks and a lot more.

And now, in order to utilise all its various functionalities, we have to import the module to our script. This can be done by using the below line in our Python script.

import mouse

And that’s it! You are all set to start working on the task at hand.

Simulating the Mouse

Let us start off by first understanding positions of the cursor on your screen.

You can receive the position of your mouse on the screen with respect to the X and Y axis by using the get_position method.

mouse.get_position()

It will print the coordinates of the point where you clicked your mouse. Something like this −

(731, 465)

You can use this method to obtain the exact locations of buttons or move your mouse to that position to perform an action.

Now that you know where your mouse must be by using the get_position, let us try moving it to a different position by using the move method.

mouse.move(150, 150)

This method moves your mouse to the "x = 150" and "y = 150" position of your screen.

You can also add in a few extra arguments to time your task.

mouse.move(150, 150, duration = 1.0)

The above line moves the mouse to (150, 150) in 1 second.

Simulating Mouse Clicks and Scrolls

Now that we’ve explored mouse movement, let us look into mouse clicks.

mouse.click('left')	# Left click
mouse.click('right')	# Right click
mouse.click('middle')	# Clicks the middle mouse button

The above methods simulate the click event on the various mouse buttons.

Now, suppose you want to perform an action every time you left click? Let's see how it's done.

mouse.on_click(lambda: print("Hello there!."))

Now, every time you left click, it will print the following on the console−

"Hello there!".

You can use the "wheel" method to simulate scrolls. Look at the following examples −

mouse.wheel(5)
mouse.wheel(-3)

Positive values help you scroll up, while negative value helps you scroll down.

Conclusion

You now know how to simulate mouse movements and clicks using Python’s mouse module. There are several other alternatives to do this, pynput or PyAutoGUI are some of the alternatives with really good features, but the mouse library offers simple methods that anyone can use with ease.

Updated on: 24-Aug-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements