How to Control your Mouse in Python

Controlling computer mouse in python. Hooking events, registering hotkeys, simulating mouse movements and click, and much more.
  · 3 min read · Updated jun 2022 · General Python Tutorials

Want to code faster? Our Python Code Generator lets you create Python scripts with just a few clicks. Try it now!

Disclosure: This post may contain affiliate links, meaning when you click the links and make a purchase, we receive a commission.

Controlling the computer mouse in code is a handy task, as it can be helpful for desktop automation, making useful desktop agents, etc. In this tutorial, you will learn how you can control the mouse in Python.

We gonna be using the convenient mouse library, let's install it:

$ pip3 install mouse

This module helps us take full control of our mouse, such as hooking global events, registering hotkeys, simulating mouse movement and clicks, and much more!

First, let's see how we can simulate mouse clicks:

import mouse

# left click'left')

# right click'right')

# middle click'middle')

Note: It is suggested to run these statements individually in a Python interactive shell such as a Jupyter notebook or IPython.

The function does what its name suggests, it sends a click with the given button, try it out!

Second, you can also get the current position of the mouse:

In [22]: mouse.get_position()
Out[22]: (646, 407)

You can drag something with the mouse:

# drag from (0, 0) to (100, 100) relatively with a duration of 0.1s
mouse.drag(0, 0, 100, 100, absolute=False, duration=0.1)

Setting absolute equal to False with (0, 0) start positions mean that it drags from the current position to 100 farther (in x and y).

Test this on a file you want to drag on your desktop!

Next, you can also determine whether a button is pressed:

# whether the right button is clicked
In [25]: mouse.is_pressed("right")
Out[25]: False

You can also move the mouse:

# move 100 right & 100 down
mouse.move(100, 100, absolute=False, duration=0.2)

This will move the mouse relatively for a duration of 0.2 seconds.

You can also make callbacks that are called whenever an event occurs, such as a mouse click:

# make a listener when left button is clicked
mouse.on_click(lambda: print("Left Button clicked."))
# make a listener when right button is clicked
mouse.on_right_click(lambda: print("Right Button clicked."))

The above code makes simple callbacks whenever the mouse's buttons are clicked, here we just used lambda functions for demonstration purposes, you are free to use any function to do whatever you want.

If you want to remove the listeners, you can call unhook_all() to remove all the listeners:

# remove the listeners when you want

You can also control the mouse wheel, let's scroll down:

# scroll down

Scrolling up:

# scroll up

Finally, you can record all mouse events and then replay them:

# record until you click right
events = mouse.record()

This will record all mouse events until the right button is pressed. Then it returns a list of events recorded, let's replay them:

# replay these events[:-1])

The reason I set events[:-1] instead of all events is that I don't want to play the right button click.

Here are some ideas you can do with this module:

  • Making Reinforcement learning agents playing video games.
  • Automating the boring desktop stuff.
  • Much more!

You can combine this with controlling your keyboard in Python and let's see what you can build with these!

Dive Deeper with Python

Finally, if you're a beginner and want to learn Python, I suggest you take the Python For Everybody Coursera course, in which you'll learn a lot about Python, good luck!

Alright, we are done! Check the full code.

Learn also: How to Record your Screen in Python.

Happy Coding ♥

Want to code smarter? Our Python Code Assistant is waiting to help you. Try it now!

View Full Code Auto-Generate My Code
Sharing is caring!

Read Also

Comment panel

    Got a coding query or need some guidance before you comment? Check out our Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips!