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.
Table of Contents
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
from pynput.mouse import Button, Controller from pynput.keyboard import Listener, KeyCode import time import threading startend_key = KeyCode(char='i') exit = KeyCode(char='t') class ClickMouse_Class(threading.Thread): def __init__(self): super(ClickMouse_Class, self).__init__() self.delay = 0.1 self.button = Button.left self.running = False self.program_run = True def start_clicking(self): self.running = True def stop_clicking(self): self.running = False def exit(self): self.stop_clicking() self.program_run = False def run(self): while self.program_run: while self.running: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1) mouse = Controller() thread = ClickMouse_Class() thread.start() def fn_on_press(key): if key == startend_key: if thread.running: thread.stop_clicking() else: thread.start_clicking() elif key == exit: thread.exit() listener.stop() with Listener(on_press=fn_on_press) as listener: listener.join() |
You need to press key
i
to start auto clicker andt
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
andexit
are keys read from the keyboard using theKeyCode()
function to provide the start, stop and exit keys for the auto clicker threads. - We create a class called
ClickMouse_Class
extending thethreading.Thread
class with several methods to control the threads. - A delay of
0.1
is also specified between clicks using thetime.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()
calledmouse
is created and the class we created is initiated and we create and start the required thread using thestart()
function. - The
fn_on_press()
function will be executed on pressing the keys, and it is collected by theListener()
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.
Further reading:
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,
1 2 3 4 5 6 7 8 9 10 |
import pyautogui import time def click_fn(): time.sleep(0.1) pyautogui.click() for i in range(20): pyautogui.moveTo(1000, 300) click_fn() |
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 thetime.sleep()
function, and click the mouse button using thepyautogui.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.
1 2 3 4 5 6 7 8 9 10 11 |
import win32api, win32con import time def click_fn(x,y): time.sleep(0.1) win32api.SetCursorPos((x,y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0) for i in range(20): click_fn(1000,300) |
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 forMOUSEEVENTF_LEFTDOWN
and then forMOUSEEVENTF_LEFTUP
.
That’s all about how to make auto clicker in Python.