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.


import_contacts

You may also like:

Related Posts

  • Python get year from date
    23 January

    Get year from Date in Python

    Table of ContentsDates in PythonWays to get year from date stored in a datetime object in PythonUsing the year attributeUsing the strftime() functionUsing the pandas.to_datetime functionWays to get year from date stored as a string in PythonUsing the for loopUsing the re.search()Conclusion In this post, we will see how to get year from Date in […]

  • 22 January

    Replace space with underscore in Python

    Table of ContentsWays to replace space with underscore in PythonUsing the for loopUsing the replace() functionUsing the re.sub() functionUsing the split() and join() functionConclusion Strings are an essential data type in programming. In Python, we can treat strings as an iterable of characters, and can perform a variety of functions and operations on them. Replacing […]

  • Python copy file to another directory
    17 January

    How to copy file to another directory in Python

    Table of ContentsWays to copy file to another directoy in PythonUsing file handlingUsing the shutil libraryUsing the pathlib libraryUsing the os moduleUsing the subprocess moduleConclusion In this article, we will see different ways to copy file to another directory in Python. We can read and write files in Python. We can also work with paths […]

  • Check if variable is String in Python
    13 January

    Check if variable is String in python

    Table of ContentsHow to check if a given variable is of the string type in Python?Using the isinstance() function.Using the type() function.Check if function parameter is String In this post, we will see what is a string in Python and how to check whether a given variable is a string or not. There are many […]

  • Count the number of characters in a String in Python
    13 January

    Count number of characters in a string in python

    Table of ContentsWays to count the number of characters in a string in PythonUsing the len() functionUsing the for loopUsing the collections.Counter classConclusion In this post, we will see how to count number of characters in a String in Python. We can think of strings as a collection of characters, with every character at a […]

  • Pandas apply function to column
    12 January

    Pandas apply function to column

    Table of ContentsHow do I apply function to column in pandas?Using dataframe.apply() functionUsing lambda function along with the apply() functionUsing dataframe.transform() functionUsing map() functionUsing NumPy.square() function We make use of the Pandas dataframe to store data in an organized and tabular manner. Sometimes there, is a need to apply a function over a specific column […]

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.