Python 3 bot for beginners: automate your computer with clicks and keyboard input

The goal of this tutorial is to build several scripts for task automation through mouse clicks or through keyboard reading and key capture in Python 3.

You can download the repository with the example code for the tutorial at https://github.com/al118345/Ejemplo_bot_python.

Use automation responsibly

The examples are intended for local productivity tasks: repeating clicks in your own environment, checking coordinates and understanding how keyboard listeners work. A keyboard listener can capture sensitive information, so it should never be used on other people's machines, shared sessions or systems where the user has not given explicit permission.

If the automation has to interact with a web application, it is often better to use a controlled API or a test tool. The article complements the guides about Angular API calls and Google reCAPTCHA in Angular, where automation and protection mechanisms appear from the web side.

Installation

To install this project, the only thing you need is the pynput library in your environment. To do so, run the following command:

Example 1. Show the click position

The following code simply prints in the terminal the coordinates where the user clicked.

In order to keep the example simple, a counter was created so the click is captured only the number of times we want. In the example, that number is 4. During those 4 iterations, a function called on_click is associated with the on_click event, as shown below.

This function only displays the coordinates where the click was made, as shown in the following code.

You can download the repository with the example code for the tutorial at https://github.com/al118345/Ejemplo_bot_python/blob/main/ejemplo_posicion.py and watch the following video with the explanation of the script:

Example 2. Click the coordinates obtained in example 1

Once we know which coordinates must be pressed, we can create the following script so the desired areas are clicked automatically. The code is the following:

As you can see, the most important part is the click_raton_posicion function which, given coordinates, moves the mouse to that place, presses the left button and releases it. After that, it pauses for 10 seconds.

You can download the repository with the example code for the tutorial at https://github.com/al118345/Ejemplo_bot_python/blob/main/ejemplo_ejecucion_bot_click.py and watch the following video with the explanation of the script:

Example 3. Store all pressed keys in a text file

Basically, the task we are going to perform is to collect all the keys pressed on your computer and store them in a text file when the program is stopped.

The first important part of the code is the listener shown below. It is responsible for invoking the on_press method whenever a key is pressed.

The following try: except: is also important because it is responsible for storing the collected information in a text document.

Finally, the on_press function stores the pressed characters whenever possible. To do so, a globally stored text string is used.

You can download the repository with the example code for the tutorial at https://github.com/al118345/Ejemplo_bot_python/blob/main/ejemplo_lectura_teclado_a_fichero.py and watch the following video with the explanation of the script: