Control Mouse in Python

Python | Controlling Mouse: Here, we will see how we can control the mouse with the help of Python?
Submitted by Abhinav Gangrade, on June 20, 2020

Modules used:

In this script, we will use PyAutoGui module.

PyAutoGui module:

PyAutoGui is a Python module that will provide us various functions that helps us to control mouse and keyboard, as it is a third party module we need to install.

Download the module:

General Way to install:
pip install PyAutoGui

Pycharm Users:
Go to the project Interpreter and Install the module.

Functions provided by this module to automate the mouse,

pyautogui.size():

  • This function will return us the size of the screen
  • Let's store the size in a variable (let's take the variable to be the size).
  • Now if we want the width of the screen then we will do.
  • size[0], this will return us the screen width,
  • size.width will also return the width of the screen
  • size.height will return us the height of the screen.

pyautogui.moveTo(100,100,duration=0.25): This function will move your cursor to (100,100) coordinate of the screen in a time of 0.25 seconds.

pyautogui.move(100,100,duration=0.25): This function will move your cursor to the (100,100) coordinate relative to your current position.

pyautogui.position(): This function will give you the current position of your cursor.

pyautogui.click(100,100): This function will move our cursor at this coordinate and will automatically click at that position and if you didn't fill anything inside the click function then it will click at the current cursor position.

pyautogui.scroll(100): This function will scroll the screen according to the value given if positive given then it will scroll upward and if negative given then it will scroll down as it will work relative to our position(means we are at (0,0)).

Now we will create a program in which we will cover all the functions.

# importing the module
import pyautogui

# storing the size of the screen
size=pyautogui.size()
print(size)
print(size.width)
print(size.height)

# Now we will create loop that will move the cursor
# this loop will create a square by just 
# moving the cursor
# so in one second we will create 1 square
for i in range(10):
    # moving to (100,100) in 0.25 sec
    pyautogui.moveTo(100,100,duration=0.25)
    pyautogui.moveTo(200,100,duration=0.25)
    pyautogui.moveTo(200,200,duration=0.25)
    pyautogui.moveTo(100,200,duration=0.25)

# Now we will move the square relative to our current
# cursor position
for i in range(10):
    # moving to (100,100) in 0.25 sec
    pyautogui.move(100,100,duration=0.25)
    pyautogui.move(200,-100,duration=0.25)
    pyautogui.move(-200,200,duration=0.25)
    pyautogui.move(100,200,duration=0.25)

# get the current position of the cursor
print(pyautogui.position())

# click the screen by just writting the code
pyautogui.click(100,200)

# scroll the screen
pyautogui.scroll(100)

Output:

Size(width=1366, height=768)
1366
768
Point(x=100, y=200)

This is how we can automate our mouse with the help of Python.


Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.