Change language

Automating Mouse and Keyboard Using Python

| |

This article shows you how to automate mouse and keyboard movements using the pyautogui module in python. This module is not preloaded from python. Therefore, to install it, run the following command:

 pip install pyautogui 

Controlling mouse movements using the pyautogui module

Python tracks and controls the mouse using the screen coordinate system. Assuming your screen resolution is 1920X1080, then your screen coordinate system looks like this:

  • size (): this function is used to get the screen resolution.

    import pyautogui

    print ( pyautogui.size ())

    Save this file with the extension. py and then run the file. 
    This Python code uses the size () function to output your screen resolution in x, y format:
    Output:

     (1920, 1080) 

    Note. Some of the codes presented in this article may not work in the pythonengineering IDE because the pythonengineering IDE does not require modules to run these codes. But this code can be easily run locally on your machine by installing python and following the instructions in the article.

  • moveTo (): use this function to move the mouse to pyautogui module.

    import pyautogui

    pyautogui.moveTo ( 100 , 100 , duration = 1 )

    This code uses the moveTo () function, which takes x and y coordinates, and optional duration argument. This function moves the mouse pointer from the current location to the x, y coordinates, and this takes the time specified in the duration argument. Save and run this python script to see the mouse pointer magically move from its current location to coordinates (100, 100), and takes 1 second in the process.

  • MoveRel () function: moves the mouse pointer relative to its previous position.

    import pyautogui

    pyautogui.moveRel ( 0 , 50 , duration = 1 )

    This the code will move the mouse pointer to (0, 50) relative to its original position. For example, if the mouse position before running the code was (1000, 1000), then this code will move the pointer to coordinates (1000, 1050) for 1 second.

  • position () : function to get the current position of the mouse pointer.

    import pyautogui

    print (pyautogui.position ())

    Output: the coordinates where your mouse was while executing the program.

  • click (): function used to click and drag the mouse.

    import pyautogui

    pyautogui.click ( 100 < code class = "plain">, 100 )

    This code performs a typical mouse click at this location (100, 100). 
    We have two functions related to the mouse drag operation, dragTo and dragRel . They perform the same functions as moveTo and moveRel, except that they hold down the left mouse button while moving, thereby initiating dragging. 
    This functionality can be used in various places, such as moving a dialog box or automatically drawing something using the Pencil tool in MS Paint. To draw a square in paint:

    import time

     
    # a module that has functions related to time.
    # Can be installed with cmd command:
    # Installation time for pip, same as pyautogui.

    import pyautogui

    time.sleep ( 10

      
    # stops program execution for 10 seconds

    pyautogui.moveTo ( 1000 , 1000 , duration = 1

     
    # moves the mouse 1000, 1000.

    pyautogui.dragRel ( 100 , 0 , duration = 1 )

     
    # drags the mouse 100, 0 relative to the previous position,
    # thus dragging it to 1100, 1000

    pyautogui.dragRel ( 0 , 100 , duration = 1 )

    pyaut ogui.dragRel ( - 100 , 0 , duration = 1 )

    pyautogui.dragRel ( 0 , - 100 , duration = 1 )

    Before running the code, open MS Paint in the background with the pencil selected. Now run the code, quickly switch to MS paint under 10 seconds (since we gave a 10 second pause time using the sleep () function before starting the program). 
    After 10 seconds, you will see a square drawn in MS paint with its top-left edge at 1000, 1000 and an edge length of 100 pixels.

  • scroll (): the scrolling function takes up no. pixels as an argument and scrolls the screen to the specified number of pixels.

    import pyautogui

    pyautogui.scroll ( 200 )

    This code scrolls the active screen to 200 pixels.

  • typewrite (): You can automate string input using the typewrite () function. just pass the string you want to enter as an argument to this function.

    import pyautogui

    pyautogui.click ( 100 , 100 )

    pyautogui.typewrite ( " hello Geeks! " )

    Assuming the text box was present at coordinates 100, 100 on the screen, then this code will click the text box to make it active and type “hello Geeks!” in it.

  • Passing key names: You can pass key names separately through the typewrite () function.

    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 the hotkey function () to press keyboard shortcuts like ctrl-c, ctrl-a, etc. Example:

    import pyautogui

    pyautogui.hotkey ( "ctrlleft" , "a" )

    This code is the automatic equivalent of pressing the left ctrl and "a" at the same time. So in windows this will select all the text on the screen.

This article is provided by tkkhhaarree . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting the article [email protected] ... See your article appearing on the Python.Engineering homepage and help other geeks.

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically