Control mouse with Python Program

In this tutorial, we will learn how to automate our mouse movements using Python code. Python provides us with a module pyautogui which has many functions which can help us to automate the mouse movements. This module doesn’t come with basic Python so we have to install this externally. Installation of this module is quite easy. Pass the below command

pip install pyautogui

in the command prompt. You must have an active internet connection during installation. Using functions of this library one can automate mouse movements. It’s quite cool when you witness such an amazing feature.  Let’s see the functions of this module.

size() in pyautogui

This function tells the size of your computer screen in pixels.

import pyautogui as py
print(py.size())

This will show you the size of your screen.

moveTo() function of pyautogui to move the cursor point in Python

This is an interesting function. moveT0(x,y, duration), as you can see it takes three arguments, the x coordinate, y coordinate and the duration. What this function does is, it moves the mouse pointer to the specified location in the specified duration.

import pyautogui as py
py.moveTo(500,500, duration = 1)

position()

This function tells the current position of the mouse pointer.

import pyautogui as py
print(py.position())

click()

This function gives mouse click location

import pyautogui as py
py.click(100, 500)

scroll()

This function automatizes the scroll function. It scrolls the page to the specified pixels.

import pyautogui as py
py.scroll(500)

typewrite()

This function can type the specified data on the screen.

import pyautogui as py 
py.click(500,500) 
py.typewrite("CodeSpeedy")

This is how we can easily operate the mouse functions using the Python program. This is quite interesting and amusing.

Also check: Create an auto typer in Python

Leave a Reply

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