While writing code, we mainly use the keyboard and mouse for input. Although the use of mouse and keyboard is similar for all the systems, we can tweak some of its functionalities using code. We can write a Python script that can specify new hotkeys or shortcuts for the keyboard and automate mouse clicks.
In this tutorial, we will learn how we can use the Python mouse module to control our mouse. Instead, if you want to know how to control a keyboard using Python, click here .
Before we dive into the Python program, we need to install the mouse library in Python first.
The Python
mouse
Library
mouse
is a lightweight Python library that is used to control the mouse for Windows and Linux systems. Using this Python library, we can hook mouse global events, hotkeys and tweak its click events. To install the library for your Python environment, run the following pip install command.
pip install mouse
How to Control your Mouse in Python?
To make this simple Python tutorial simple to understand, since a computer mouse helps to perform many things, we have divided the code into various sections depending on the operation that we need to perform with the mouse. So, here goes:
1) Simulate a Mouse Click with Python
Let's say you are building a Python application where you want if the user hovers over some special element; it automatically clicks one of the mouse buttons. So let's begin with how can we simulate the mouse and click its button using Python:
import mouse
# click left mouse button
mouse.click('left')
# click right mouse button
mouse.click('right')
# click middle mouse button
mouse.click('middle')
The mouse
click()
function is used to simulate the mouse button click. We recommend running the above script in an interactive online Python editor, such as
Jupyter Notebook
and OnlineGDB.
2) Get the Location of the Mouse Cursor using Python
Let's say you want to get the current location or position of your mouse cursor. For that, you can use the
get_position()
function that returns the width and height of the mouse in the form of a tuple as output:
import mouse
print(mouse.get_position())
Output
(598, 411) #width height
3) Drag the Mouse Cursor using Python
The mouse module also provides a function that can drag the mouse cursor from one location to another. To d0 the same using Python, we can use the drag() function:
import mouse
#from (0,120) to (1000, 250) absolute screen in 1 sec
mouse.drag(0,120 , 1000, 250 ,absolute=False, duration=1)
In the code above, the
drag()
function will drag the mouse cursor from (0,120) pixels to (1000, 250) pixels in one second.
4) Move the Mouse Cursor using Python
The
drag()
function is a combination of left-click and mouse movements, and it will select the text in the direction it was moved. But if you only want to move the cursor from one location to another, you can use the
move()
function.
import mouse
#move 1000px right and 500px down
mouse.move(1000,500, absolute=False, duration=1)
5) Check the Mouse Click
To check if the user clicks the right, left, or center button of the mouse, we can use the
is_pressed()
function.
import mouse
#click right button?
print(mouse.is_pressed("right"))
#click left button?
print(mouse.is_pressed("left"))
#click center button?
print(mouse.is_pressed("center"))
Output
False
False
False
6) Action on Mouse Click
If you want to trigger an event when the user clicks either of the mouse buttons, you can use
on_click()
, and
on_right_click()
functions. These two functions accept a function that triggers when the user clicks the mouse button.
import mouse
import keyboard #pip install keyboard
#when left button click
mouse.on_click(lambda : print("Left Button was clicked"))
#when right button click
mouse.on_right_click(lambda : print("Right Button was clicked"))
#press Esc to kill the event
if not keyboard.wait("Esc"):
mouse.unhook_all()
When you press the
Esc
button, the
unhook_all()
function will remove all the event listeners.
7) Control the Wheel of the Mouse
By far, we have discussed how we can control or simulate the movement and button clicks of a computer mouse. Now let's see how we can control the mouse scroller using a Python script . To control the mouse wheel, we use the wheel() function. The wheel function accepts integer values representing the number of scrolls. The -ve number indicates scrolling down, and the +ve integer indicates scrolling up.
import mouse
# scroll up
mouse.wheel(1)
# scroll down
mouse.wheel(-1)
Conclusion
In this Python tutorial, we learned how we can control a mouse using Python. In this tutorial, we have only discussed a few of the mouse functions, but the mouse module supports many others.
To know more about all the functions provided by the mouse module, read its official documentation on GitHub .
People are also reading:
- Detect Shapes in Python
- Python Data Types
- Comments, Statements, Indentation & Docstring in Python
- Python Name, Namespace, & Scope
- Python if, if…else, if…elif…else & Nested if Statement
- Functions Arguments in Python
- Python Function
- Continue & Break Keywords in Python
- Python Closures
- Decorators in Python
Leave a Comment on this Post