Mouse and Keyboard Automation Using Python

Introduction:

In this tutorial, we are learning about mouse and keyboard automation using Python. The Pyautogui library is used here. This library allows you to control mouse and keyboard functions. It is a cross-platform GUI automation Python module for humans. Since it is a third-party library. So, we need to install it in your system. The pip command is given below -

Mouse automation using Python:

Here, we give a program that is used to move the mouse automatically. When you run the program, you can see the mouse movement for each command. I ran the following command in CLI to capture mouse movement. You can try other variations. Here, we use the size() function, which is used to get the screen resolution.

Program code:

Here, we give a program to perform the mouse automation by using the size() function in Python. The code is given below -

Output:

The following Python code uses the size() function to display the solution in x and y format. Now, we compile the above code to get the screen resolution. Then, after running the program, we find the result. The finding output is given below -

(1430, 940)

Program code:

Here, we give a program to perform the mouse moving by using the moveTo() function in Python. The code is given below -

Output:

The above code uses the moveTo() function. It takes x and y coordinates and an optional time argument. This function moves the mouse pointer from its current location to the x and y coordinates and requires the time specified by the timer to complete. When you save and run this Python script, you will see the mouse pointer magically moves from its current position to coordinates (10, 10), which will take a duration of 2.5 seconds.

Program code:

Here, we give a program to perform the mouse pointer moving relative to its previous position by using the moveRel() function in Python. The code is given below -

Output:

The above code uses the moveRel() function. This function moves the mouse pointer relative to its previous position to the x,y coordinate and requires the time specified by the timer to complete. The above code will move the mouse pointer to (150, 0) relative to its original position. For example, if the mouse position is (1000, 1000) before running the code, this code will move the cursor to the control point (1150, 1000) within 1.5 seconds duration.

Program code:

Here, we give a program to find the mouse position by using the position() function in Python. The code is given below -

Output:

The above code uses the position() function. Coordinates of the mouse when running the program.

Program code:

Here, we give a program to perform the mouse clicking and dragging using the click() function in Python. The code is given below -

Output:

The above code uses the click() function. This code causes the mouse to be pressed at position (150, 70).

We have two mouse-related functions, which are dragTo and dragRel. Their execution is similar to the moveTo and moveRel functions, except that they start dragging by holding down the left button of the mouse while moving it. This can be used in many places, like moving a dialog box or cutting something using the stylus in MS Paint.

Program Code:

Here, we give a program code to draw a square in MS Paint by using the dragRel function in Python. The code is now given below -

Output:

Before running the above code, open MS Paint in the background and select the pencil tool. Now run the code and switch to MS Paint before 15 seconds. We stopped using the sleep() function for 15 seconds before ending the program. After 15 seconds, you will see that a square has been drawn in MS Paint with a left edge of 1000, 1000, and a side length of 100 pixels.

Program code:

Here, we give a program to perform the mouse scrolling by using the scroll() function in Python. The scroll function requires a number of pixels as parameters and scrolls the screen by the number of pixels. The code is given below -

Output:

The above code uses the scroll() function, which scrolls the active screen by up to 500 pixels.

Keyboard automation using Python:

The main function of the keyboard is the typewrite(). This function will write the characters from the previous line. To add a delay between each key character, pass an int or float to the time keyword argument.

Program Code:

Here, we give a program code for keyboard automation in Python by using the typewrite() function. The code is given below -

Output:

The above code uses the typewrite() function. Assuming there is 100 or 90 text on the control screen, this code will click to activate the text and say "Good Morning, Everyone" into this.

Program Code:

Here, we give another program code for keyboard automation in Python by using the typewrite() function. We can pass key names separately. The code is given below -

Output:

The above code uses the typewrite() function. This code is equivalent to typing "c", pressing the right arrow, and then pressing the right control button.

Program Code:

Here, we give another program code for keyboard automation in Python by using the hotkey() function. Use the hotkey() function to press the key combination such as ctrl-c, ctrl-x, ctrl-a, etc. The code is given below -

Output:

The above code uses the hotkey() function. This code is equivalent to pressing the right Ctrl and "c" keys at the same time. So, on Windows, this will copy all text visible on the screen.

Program Code:

Please send a message on WhatsApp and have it deleted for everyone. Whatsapp must be open in Chrome to run. After running this code, open the WhatsApp tab in Chrome. The code is given below -

Conclusion:

In this tutorial, we learn about mouse and keyboard automation using Python and some functions that can be used for this purpose.