Python 3 Pynput Mouse Auto Clicker Bot Automation Script With Start and Stop Hotkeys Full Project For Beginners

Python 3 Pynput Mouse Auto Clicker Bot Automation Script With Start and Stop Hotkeys Full Project For Beginners

 

Welcome folks today in this post we will be building a mouse autoclicker bot automation script in python using pynput library. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to install the following library using the pip command as shown below

 

pip installl pynput

 

After installing this library make an app.py file and copy paste the following code

 

app.py

 

 

# importing time and threading 
import time 
import threading 
from pynput.mouse import Button, Controller 

# pynput.keyboard is used to watch events of 
# keyboard for start and stop of auto-clicker 
from pynput.keyboard import Listener, KeyCode 


# four variables are created to 
# control the auto-clicker 
delay = 0.001
button = Button.right 
start_stop_key = KeyCode(char='a') 
stop_key = KeyCode(char='b') 

# threading.Thread is used 
# to control clicks 
class ClickMouse(threading.Thread): 
    
# delay and button is passed in class 
# to check execution of auto-clicker 
    def __init__(self, delay, button): 
        super(ClickMouse, self).__init__() 
        self.delay = delay 
        self.button = button 
        self.running = False
        self.program_running = True

    def start_clicking(self): 
        self.running = True

    def stop_clicking(self): 
        self.running = False

    def exit(self): 
        self.stop_clicking() 
        self.program_running = False

    # method to check and run loop until 
    # it is true another loop will check 
    # if it is set to true or not, 
    # for mouse click it set to button 
    # and delay. 
    def run(self): 
        while self.program_running: 
            while self.running: 
                mouse.click(self.button) 
                time.sleep(self.delay) 
            time.sleep(0.1) 


# instance of mouse controller is created 
mouse = Controller() 
click_thread = ClickMouse(delay, button) 
click_thread.start() 


# on_press method takes 
# key as argument 
def on_press(key): 
    
# start_stop_key will stop clicking 
# if running flag is set to true 
    if key == start_stop_key: 
        if click_thread.running: 
            click_thread.stop_clicking() 
        else: 
            click_thread.start_clicking() 
            
    # here exit method is called and when 
    # key is pressed it terminates auto clicker 
    elif key == stop_key: 
        click_thread.exit() 
        listener.stop() 


with Listener(on_press=on_press) as listener: 
    listener.join()

 

See also  Python 3 OpenCV Script to Build Screen or Window Capture Using Pillow & Numpy Library GUI Desktop App Full Project For Beginners

 

Now you can modify some parameters inside this mouse autoclicker script to customize this

 

You can modify the start hotkey which is set to a which will start the autoclicker

 

See also  Python 3 Tkinter FontSelectFrame Widget to Select Fonts Using Font Chooser or Dialog Window GUI Desktop App Full Project For Beginners

You can modify the stop hotkey which is set to b which will stop the autoclicker

 

You can modify the delay in terms of seconds to customize the delay between mouse clicks

 

After doing this customization now to run this python script just type the below command to run the mouse autoclicker

 

python app.py

 

Now to start the mouse autoclicker press the a button on the keyboard and it will click indefinately and to stop it press b button on the keyboard

See also  Python 3 Turtle Library Gomoku (Five in a Row) Board Strategy Game GUI Desktop App Full Project For Beginners

 

 

Now to test the mouse clicker you can go to this openprocessing.org to count the no of clicks of mouse clicker bot

 

 

 

You can see I have made 2810 clicks in 2 seconds only

Leave a Reply