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.

Related Posts

  • Python add commas to number
    11 September

    Python add commas to number

    Table of ContentsUsing the format() function to add commas to numbers in PythonUsing the fstrings to add commas to numbers in PythonUsing the regular expressions to add commas to numbers in PythonUsing the locale module to add commas to numbers in Python Python allows us to format values to get the final result in our […]

  • Write array to CSV in Python
    30 August

    Python write array to CSV

    Table of ContentsUsing the pandas.to_csv() function to write array to CSV fileUsing the numpy.tofile() function to write array to CSV fileUsing the numpy.savetxt() function to write array to CSV fileUsing the file-handling methods to write array to CSV fileUsing the writerows() function to write array to CSV file A CSV file is a simple text […]

  • Multiply in Python
    30 August

    Multiply in Python

    Table of ContentsMultiply operator in PythonMultiplying a string with an integerMultiplying all the elements of a listUse the for loop to multiply all the elements of a listUse the functools.reduce() function to multiply all the elements of a listUse the numpy.prod() function to multiply all the elements of a listUse the math.prod() function to multiply […]

  • Iterate through files in directory in Python
    30 August

    Iterate through files in directory in python

    Table of ContentsUsing the os.scandir() function to iterate through files in a given directory in Python.Using the os.listdir() function to iterate through files in a given directory in Python.Using the os.walk() function to iterate through files in a given directory in Python.Using the pathlib module to iterate through files in a given directory in Python.Using […]

  • Write variable to file in Python
    30 August

    Write variable to file in python

    Table of ContentsUsing the str() functionUsing the repr() functionUsing the string formattingUsing the pickle.dump() functionUsing the numpy.savetxt function File handling is an important concept of programming. Python allows us to read and write content to an external file. In this article, we will discuss several ways to write variable to file in Python. First, let […]

  • Inverse matrix in Python
    24 August

    Inverse matrix in python

    Table of ContentsUsing the Gauss-Jordan method to find the inverse of a given matrix in Python.Using the numpy.linalg.inv() function to find the inverse of a given matrix in Python. The inverse of a matrix in simple mathematics can be defined as a matrix, which when multiplied by the original matrix, gives out the Identity matrix. […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.