Mouse Automation in Python using PyAutoGUI

In this tutorial, we’ll learn about Mouse Automation in Python using PyAutoGUI. PyAutoGUI is a module in python which can automate Mouse movements as well as keystrokes. Mouse automation in python using PyAutoGUI has many applications in robotics. To read more about this amazing module, go visit its documentation.

So let’s get started!

Installing PyAutoGUI

Installing the module in Windows is pretty easy. Just type in the following into your command line…

pip install pyautogui

Learning basic functions

First, let’s import the PyAutoGUI module.

import pyautogui as gui

Then, check the size of our screen. this is done so that the mouse coordinates never go out of range.

gui.size()

Output:

Size(width=1920, height=1080)

Now we’ll see how to change the current position of our cursor. This is done using the moveTo() function. The first two parameters are the coordinates to be moved to, and the duration parameter is the time to be taken by the cursor to go to that position. The moveTo() function can also be replaced by the moveRel() function which moves the cursor to a relative position with respect to its current position.

gui.moveTo(0,0,duration=1)
gui.moveRel(0,-300,duration=1)

We can also determine the current position of the mouse through the position() function. Let’s see a small program to help us understand this better.

try:
    while True:
        x_cord,y_cord=gui.position()
        print("x={}   y={}".format(x_cord,y_cord))
except KeyboardInterrupt:
    print("Exit")

This bit of code will continuously print the current position of the mouse until CTRL+C is pressed.

Output:

x=1354   y=540
x=1354   y=540
x=1288   y=226
x=1288   y=226
x=1331   y=269 
x=1331   y=269Exit

Initiating mouse clicks for Mouse Automation in Python

There are functions for left-click, right-click, dragging, scrolling and for many more events. Here are some of them:

gui.click()        #left-click
gui.doubleClick()  #double-click
gui.rightClick()   #right-click
gui.dragTo(0,0)    #drags from current position to (0,0)
gui.dragRel(0,100) #drags down 100 pixels

To better understand these functions, let’s write a program. We will draw a picture using just PyAutoGUI.

gui.moveTo(200,400,duration=1)
gui.click()
for i in range(5):
    gui.dragRel(100,0,duration=1)
    gui.dragRel(0,100,duration=1)

First, we go to our starting position from where we want our drawing to start. Then, the click() function is used to bring the window into focus. Further, we loop the dragRel() functions 5 times. The first dragRel() goes right by 100 pixels, and the second one goes down by 100 pixels. Thus creating a stairs-like figure.

Output:

Mouse Automation in Python using PyAutoGUI

Also read:

KNN Classification using Scikit-Learn in Python

Leave a Reply

Your email address will not be published. Required fields are marked *