This is a script that enables you to click your computer mouse repeatedly through a little delay. It functions on Windows, Mac and Linux and can be regulated with user-defined keys.

You are watching: How to make an auto clicker in notepad


What is this?

This project provides the cross-platform module pynput to control the mouse and also monitor the key-board at the exact same time to develop a simple auto clicker.

PIP

If girlfriend haven"t supplied or setup pip before, look at my accuse on how to setup python"s pip come setup pip.

Installing Pynput

We will certainly be using the pynput module to listen to mouse events. To download this module execute pip download pynput in cmd. Watch the calculation to make sure no errors have actually occurred; it will tell you as soon as the module has actually been successfully installed.

*

To double-check the it was mounted successfully, open up up IDLE and also execute the command import pynput; no errors should occur.

*
.

Writing the Code

First, we have to import time and also threading. Then import Button and also Controller native pynput.mouse so we can manage the mouse and also import Listener and KeyCode from pynput.keyboard for this reason we deserve to watch for keyboard occasions to start and stop the auto clicker.


import timeimport threadingfrom pynput.mouse import Button, Controllerfrom pynput.keyboard income Listener, KeyCode
Next create four variables as presented below. "delay" will be the hold-up between each button click. "button" will certainly be the switch to click, this deserve to be one of two people "Button.left", "Button.right" or even "Button.middle". "start_stop_key" is the an essential you want to use to start and also stop the auto clicker. Ns have set it to the vital "s" to do it nice and also simple, you can use any crucial here. Finally, the "exit_key" is the vital to nearby the program collection it favor before, yet make certain it is a various key.


delay = 0.001button = Button.leftstart_stop_key = KeyCode(char='s')exit_key = KeyCode(char='e')
Now produce a class that expand threading.Thread that will permit us to manage the computer mouse clicks. Happen they delay and button to this and also have two flags that determine whether the is to run or if the totality program is stopping.


class ClickMouse(threading.Thread): 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
Now we need to develop the an approach that is run as soon as the object starts. We have to keep looping when the program_running is true and also then create an additional loop inside the checks if the running is set to true. If we are inside both loops, click the collection button and then sleep for the collection delay.


def run(self): when self.program_running: if self.running: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1)
Now we desire to produce an circumstances of the computer mouse controller, develop a ClickMouse thread and start the to gain into the loop in the operation method.


Now produce a technique called on_press the takes a key as an argument and also setup the key-board listener.


Now modify the on_press method. If the key pressed is the very same as the start_stop_key, avoid clicking if the to run flag is collection to true in the object otherwise begin it. If the key pressed is the leave key, call the exit an approach in the thread and also stop the listener. The new method will look choose this:


def on_press(key): if crucial == start_stop_key: if click_thread.running: click_thread.stop_clicking() else: click_thread.start_clicking() elif key == exit_key: click_thread.exit() listener.stop()
This script deserve to be saved as a .pyw to operation in the background. The can easily be tho closed using the set exit vital even once no dialog is shown.

Using the Script

To usage this script set the variables at the top to what girlfriend want.

delay: They hold-up between each mouse click (in seconds)button: The mouse button to click; Button.left | Button.middle | Button.rightstart_stop_key: They an essential to start and also stop clicking. Make sure this is one of two people from the crucial class or set using a KeyCode as shown.exit_key: The crucial to avoid the program. Make certain this is one of two people from the an essential class or collection using a KeyCode as shown.

Then run the script and also use the start/stop key when wanted. Push the set exit crucial to exit.

Final Code


import timeimport threadingfrom pynput.mouse income Button, Controllerfrom pynput.keyboard import Listener, KeyCodedelay = 0.001button = Button.leftstart_stop_key = KeyCode(char='s')exit_key = KeyCode(char='e')class ClickMouse(threading.Thread): def __init__(self, delay, button): super(ClickMouse, self).__init__() self.delay = hold-up self.button = switch 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 def run(self): while self.program_running: if self.running: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1)mouse = Controller()click_thread = ClickMouse(delay, button)click_thread.start()def on_press(key): if key == start_stop_key: if click_thread.running: click_thread.stop_clicking() else: click_thread.start_clicking() elif key == exit_key: click_thread.exit() listener.stop()with Listener(on_press=on_press) together listener: listener.join()

Common Issues and also Questions

ModuleNotFoundError/ImportError: No module called "pynput"

Did you download pynput? This error will certainly not occur if you set up it properly. If you have multiple version of Python, make certain you space installing pynput on the same version together what you space running the script with.

I obtained a SyntaxError

Syntax errors are resulted in by you and also there is naught I can offer to deal with it personal from telling you to review the error. They constantly say where the error is in the output making use of a ^. Generally, world that gain this issue have not correct indentation, base in the wrong location or something spelt wrong. You deserve to read about SyntaxError on Python"s docs here.

"python" is not well-known as an interior or exterior command

Python hasn"t been mounted or it hasn"t been installed properly. Walk to /blog/post/how-to-setup-pythons-pip/ and follow the tutorial. Just prior to you enter the scripts folder right into the course variable, remove the "scripts" part at the end. You will also want to include another route with "scripts" to have pip.

See more: How Many Oranges In A Bushel ? How Many Navel Oranges Are In A Bushel


about

Owner the PyTutorials and creator that auto-py-to-exe. I gain making fast tutorials because that people new to particular topics in Python and tools that aid fix tiny things.