Mouse and keyboard automation using Python
Last Updated :
21 Jan, 2021
This article illustrates how to automate movements of mouse and keyboard using pyautogui module in python. This module is not preloaded with python. So to install it run the following command:
pip3 install pyautogui
Controlling mouse movements using pyautogui module
Python tracks and controls mouse using the coordinate system of the screen. Suppose the resolution of your screen is 1920X1080, then your screen’s coordinate system looks like this:
- size(): This function is used to get Screen resolution.
Python
import pyautogui
print(pyautogui.size())
Save this file with .py extension, and then run the file.
This python code use size() function to output your screen resolution in x, y format:
Output:
(1920, 1080)
Note: Some of the codes provided in this article might not run on geeksforgeeks IDE, since geeksforgeeks IDE doesn't have the required modules to run these codes. But these codes can be easily run locally on your PC by installing python and following the instructions provided in the article.
- moveTo(): use this function to move the mouse in pyautogui module.
Python
import pyautogui
pyautogui.moveTo(100, 100, duration = 1)
This code uses moveTo() function, which takes x and y coordinates, and an optional duration argument. This function moves your mouse pointer from it’s current location to x, y coordinate, and takes time as specified by duration argument to do so. Save and run this python script to see your mouse pointer magically moving from its current location to coordinates (100, 100), taking 1 second in this process.
- moveRel() function: moves the mouse pointer relative to its previous position.
Python
import pyautogui
pyautogui.moveRel(0, 50, duration = 1)
This code will move mouse pointer at (0, 50) relative to its original position. For example, if mouse position before running the code was (1000, 1000), then this code will move the pointer to coordinates (1000, 1050) in duration 1 second.
- position(): function to get current position of the mouse pointer.
Python
import pyautogui
print(pyautogui.position())
Output: coordinates where your mouse was residing at the time of executing the program.
- click(): Function used for clicking and dragging the mouse.
Python
import pyautogui
pyautogui.click(100, 100)
This code performs a typical mouse click at the location (100, 100).
We have two functions associated with the drag operation of the mouse, dragTo and dragRel. They perform similar to moveTo and moveRel functions, except they hold the left mouse button while moving, thus initiating a drag.
This functionality can be used at various places, like moving a dialog box, or drawing something automatically using a pencil tool in MS Paint. To draw a square in paint:
Python
import time
# a module which has functions related to time.
# It can be installed using cmd command:
# pip install time, in the same way as pyautogui.
import pyautogui
time.sleep(10)
# makes program execution pause for 10 sec
pyautogui.moveTo(1000, 1000, duration = 1)
# moves mouse to 1000, 1000.
pyautogui.dragRel(100, 0, duration = 1)
# drags mouse 100, 0 relative to its previous position,
# thus dragging it to 1100, 1000
pyautogui.dragRel(0, 100, duration = 1)
pyautogui.dragRel(-100, 0, duration = 1)
pyautogui.dragRel(0, -100, duration = 1)
Before running the code, open MS paint in the background with the pencil tool selected. Now run the code, quickly switch to MS paint before 10 seconds (since we have given 10 second pause time using sleep() function before running the program).
After 10 seconds, you will see a square being drawn in MS paint, with its top-left edge at 1000, 1000, and edge length 100 pixels.
- scroll(): scroll function takes no. of pixels as an argument, and scrolls the screen up to a given number of pixels.
Python
import pyautogui
pyautogui.scroll(200)
This code scrolls the active screen up to 200 pixels.
- typewrite(): You can automate typing of the string by using typewrite() function. just pass the string which you want to type as an argument of this function.
Python
import pyautogui
pyautogui.click(100, 100)
pyautogui.typewrite("hello Geeks !")
Suppose a text field was present at coordinates 100, 100 on-screen, then this code will click the text field to make it active and type “hello Geeks!” in it.
- Passing key names: You can pass key names separately through typewrite() function.
Python
import pyautogui
pyautogui.typewrite(["a", "left", "ctrlleft"])
This code is the automatic equivalent of typing "a", pressing the left arrow key, and pressing the left control key.
- Pressing hotkey combinations: Use hotkey() function to press the combination of keys like ctrl-c, ctrl-a, etc.
Python
import pyautogui
pyautogui.hotkey("ctrlleft", "a")
This code is the automatic equivalent of pressing left ctrl and "a" simultaneously. Thus in windows, this will result in the selection of all text present on the screen.
Example:
To send a message in WhatsApp and delete it for everyone automatically. You need to have Whatsapp already opened in chrome, to run this. After running this code, open the WhatsApp tab on chrome.
Python3
import pyautogui as pg
import time
def delete_for_everyone():
pg.click(807, 979)
pg.typewrite("hello")
pg.typewrite(["enter"])
time.sleep(2)
pg.click(1621, 896)
pg.click(1621, 896)
# time.sleep(1)
pg.click(1693, 859)
# time.sleep(1)
pg.click(1014, 669)
# time.sleep(1)
pg.click(1111, 605)
a=20
time.sleep(10)
while(a!=0):
delete_for_everyone()
a=a-1
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Lists
In Python, a list is a built-in dynamic sized array (automatically grows and shrinks). We can store all types of items (including another list) in a list. A list may contain mixed type of items, this is possible because a list mainly stores references at contiguous locations and actual items maybe s
6 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read