How to Control your Mouse in Python

Abdou Rockikz · 23 aug 2019

Abdou Rockikz · 3 min read · Updated oct 2019 · General Python Topics

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 mouse library, install it:

pip3 install mouse

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

import mouse

# left click
mouse.click('left')

# right click
mouse.click('right')

# middle click
mouse.click('middle')

Note: It is suggested to run these statements individually in a python interactive shell.

click() method 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 equals to False with (0, 0) start positions means that it drags from the current position to 100 farther (in x and y).

Test this on a file you want to drag in 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 in 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."))

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

# remove the listeners when you want
mouse.unhook_all()

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

# scroll down
mouse.wheel(-1)

Scrolling up:

# scroll up
mouse.wheel(1)

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
mouse.play(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 to play video games.
  • Automating the boring desktop stuff.
  • Much more!

You can also control your keyboard in Python !

Alright, we are done!, check the full code.

Happy Coding ♥

View Full Code
Sharing is caring!


Read Also





Comment panel

   
Comment system is still in Beta, if you find any bug, please consider contacting us here.