How to make auto clicker in Python

Auto clicker in Python

Python has inbuilt modules and methods which allow it to detect and perform keyboard and mouse inputs. We can create an auto-mouse clicker in Python.

An auto-clicker is a simple Python script that clicks the mouse repeatedly several times, as specified by the user. We can control the speed, position, and how many times we wish to click depending on our requirements.

There are several methods available to create such scripts. Let us discuss them in this article.

Using the pynput module to create an auto clicker

The pynput module allows to read and produce keyboard and mouse input using Python.

We can create a class with different methods and functionalities from this module, which can act as an auto clicker in Python.

See the code below.

You need to press key i to start auto clicker and t to stop auto clicker.

Output:
How to make auto clicker in Python – Output 1

A lot happening in the above code. Let us discuss this.

  • The startend_key and exit are keys read from the keyboard using the KeyCode() function to provide the start, stop and exit keys for the auto clicker threads.
  • We create a class called ClickMouse_Class extending the threading.Thread class with several methods to control the threads.
  • A delay of 0.1 is also specified between clicks using the time.sleep() function.
  • We specify the button to be clicked using Button.Left.
  • A method is created inside the class to keep the thread running in a loop until a required condition is met.
  • The thread keeps running until the required keys are pressed.
  • An instance of Controller() called mouse is created and the class we created is initiated and we create and start the required thread using the start() function.
  • The fn_on_press() function will be executed on pressing the keys, and it is collected by the Listener() function.
  • By pressing the exit key, we can stop the thread.

This method creates a proper auto clicker script. We can also create simple functions which can click the mouse at any provided coordinates and run it in a loop. Two such methods are discussed below.

Using the pyautogui module to create an auto clicker

The pyautogui.click() function can be used to click the mouse via Python. We can use this function to create an auto clicker by running it a given number of times.

For example,

Output:

How to make auto clicker in Python – Output 2

Now, let us understand what happens when we run the above script.

  • We first create a function called click_fn() which will first create a small time delay using the time.sleep() function, and click the mouse button using the pyautogui.click() method.
  • We set the cursor to the required position where we wish to click the mouse using the pyautogui.moveTo() method.
  • We create a loop to call this function 20 times.

Using the pywin32 (winauto32api) module to create an auto clicker

This is very similar to the previous method. The pywin32 module is a very efficient module that allows Python to deal with Windows COM objects and automation.

The mouse_event() function can be used to control and initiate mouse clicks. We will create an auto clicker using this function.

See the code below.

Output:
How to make auto clicker in Python – Output 3

In the above example,

  • The click_fn() function acts as an auto clicker and is called 20 times using the for loop.
  • In this function, we first create a small delay using the time.sleep() function.
  • The SetCurosPos() function moves the cursor to the provided coordinates.
  • The mouse_event function is called twice, once for MOUSEEVENTF_LEFTDOWN and then forMOUSEEVENTF_LEFTUP.

That’s all about how to make auto clicker in Python.

Was this post helpful?

Leave a Reply

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