- Saved searches
- Use saved searches to filter your results more quickly
- Kafels/AutoClicker
- Name already in use
- Sign In Required
- Launching GitHub Desktop
- Launching GitHub Desktop
- Launching Xcode
- Launching Visual Studio Code
- Latest commit
- Git stats
- Files
- README.md
- About
- Python Auto Clicker
- How to make auto clicker in Python
- Using the pynput module to create an auto clicker
Saved searches
Use saved searches to filter your results more quickly
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.
Basic python auto clicker
Kafels/AutoClicker
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Sign In Required
Please sign in to use Codespaces.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching GitHub Desktop
If nothing happens, download GitHub Desktop and try again.
Launching Xcode
If nothing happens, download Xcode and try again.
Launching Visual Studio Code
Your codespace will open once ready.
There was a problem preparing your codespace, please try again.
Latest commit
Git stats
Files
Failed to load latest commit information.
README.md
There’s 3 variables to modify:
BIND_KEY — The keyboard button name that going to enable/disable the auto clicker
CLICK_PER_SECONDS — How many times the button need to be pressed in a second.
BIND_MOUSE_BUTTON — What’s the button the auto clicker is going to press. 1 — Left | 2 — Right | 3 — Middle
cd /your/cloned/folder/directory run_application.bat
cd /your/cloned/folder/directory chmod +x run_application.sh ./run_application
# The keyboard key that's enable/disable the auto clicker BIND_KEY = 'F12' # How many clicks will do in a second CLICK_PER_SECONDS = 1 # What's the button that going to be pressed: 1 - Left | 2 - Right | 3 - Middle BIND_MOUSE_BUTTON = 2 import logging import os import subprocess import sys import time from threading import Thread is_windows = sys.platform.startswith('win') try: from pykeyboard import PyKeyboardEvent from pymouse import PyMouse except ModuleNotFoundError: __install_dependencies = (lambda name: subprocess.Popen(args=['python', '-m', 'pip', 'install', *name.split(' ')]).communicate()) __install_dependencies('--upgrade pip') if is_windows: __install_dependencies('pywin32') abs_path = os.path.join(os.path.dirname(__file__), 'win32', 'pyHook-1.5.1-cp36-cp36m-win32.whl') __install_dependencies(abs_path) else: __install_dependencies('Xlib') __install_dependencies('PyUserInput') finally: from pykeyboard import PyKeyboardEvent from pymouse import PyMouse class AutoClicker(PyKeyboardEvent): def __init__(self): super(AutoClicker, self).__init__() self.mouse = PyMouse() self.keycode = None if is_windows else self.lookup_character_keycode(BIND_KEY) self.enable = False self.sleep = 1 / CLICK_PER_SECONDS self.logger = self.__create_logger() def run(self): self.logger.info('AutoClick: Started') super(AutoClicker, self).run() def tap(self, keycode, character, press): if press and (keycode == self.keycode or character == BIND_KEY): self.enable = not self.enable if self.enable: self.logger.info('AutoClick: Enabled') Thread(target=self.__click).start() else: self.logger.info('AutoClick: Disabled') def __click(self): while self.enable: x, y = self.mouse.position() self.mouse.click(x, y, button=BIND_MOUSE_BUTTON) if self.enable: time.sleep(self.sleep) @staticmethod def __create_logger(): logger = logging.getLogger() logger.setLevel(logging.INFO) handler = logging.StreamHandler(sys.stdout) handler.setLevel(logging.INFO) formatter = logging.Formatter('%(asctime)s - %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) return logger AutoClicker().run()
About
Basic python auto clicker
Python Auto Clicker
This is a script that allows you to click your mouse repeatedly with a small delay. It works on Windows, Mac and Linux and can be controlled with user-defined keys.
This project uses the cross-platform module pynput to control the mouse and monitor the keyboard at the same time to create a simple auto clicker.
If you haven’t used or setup pip before, look at my tutorial on how to setup python’s pip to setup pip.
We will be using the pynput module to listen to mouse events. To install this module execute pip install pynput in cmd. Watch the output to make sure no errors have occurred; it will tell you when the module has been successfully installed.
To double-check that it was installed successfully, open up IDLE and execute the command import pynput ; no errors should occur.
.
First, we need to import time and threading. Then import Button and Controller from pynput.mouse so we can control the mouse and import Listener and KeyCode from pynput.keyboard so we can watch for keyboard events to start and stop the auto clicker.
import time import threading from pynput.mouse import Button, Controller from pynput.keyboard import Listener, KeyCode
Next create four variables as shown below. ‘delay’ will be the delay between each button click. ‘button’ will be the button to click, this can be either ‘Button.left’, ‘Button.right’ or even ‘Button.middle’. ‘start_stop_key’ is the key you want to use to start and stop the auto clicker. I have set it to the key ‘s’ to make it nice and simple, you can use any key here. Finally, the ‘exit_key’ is the key to close the program set it like before, but make sure it is a different key.
delay = 0.001 button = Button.left start_stop_key = KeyCode(char='s') exit_key = KeyCode(char='e')
Now create a class that extends threading.Thread that will allow us to control the mouse clicks. Pass they delay and button to this and have two flags that determine whether it is running or if the whole 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
Next, add the methods shown below to control the thread externally.
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 create the method that is run when the thread starts. We need to keep looping while the program_running is true and then create another loop inside that checks if the running is set to true. If we are inside both loops, click the set button and then sleep for the set delay.
def run(self): while self.program_running: while self.running: mouse.click(self.button) time.sleep(self.delay) time.sleep(0.1)
Now we want to create an instance of the mouse controller, create a ClickMouse thread and start it to get into the loop in the run method.
mouse = Controller() click_thread = ClickMouse(delay, button) click_thread.start()
Now create a method called on_press that takes a key as an argument and setup the keyboard listener.
def on_press(key): pass with Listener(on_press=on_press) as listener: listener.join()
Now modify the on_press method. If the key pressed is the same as the start_stop_key, stop clicking if the running flag is set to true in the thread otherwise start it. If the key pressed is the exit key, call the exit method in the thread and stop the listener. The new method will look like this:
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()
This script can be saved as a .pyw to run in the background. It can easily be still closed using the set exit key even when no dialog is shown.
To use this script set the variables at the top to what you want.
- delay : They delay between each mouse click (in seconds)
- button : The mouse button to click; Button.left | Button.middle | Button.right
- start_stop_key : They key to start and stop clicking. Make sure this is either from the Key class or set using a KeyCode as shown.
- exit_key : The key to stop the program. Make sure this is either from the Key class or set using a KeyCode as shown.
Then run the script and use the start/stop key when wanted. Press the set exit key to exit.
import time import threading from pynput.mouse import Button, Controller from pynput.keyboard import Listener, KeyCode delay = 0.001 button = Button.left start_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 = 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 def run(self): while self.program_running: while 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) as listener: listener.join()
Common Issues and Questions
ModuleNotFoundError/ImportError: No module named ‘pynput’
Did you install pynput? This error will not occur if you installed it properly. If you have multiple versions of Python, make sure you are installing pynput on the same version as what you are running the script with.
Syntax errors are caused by you and there is nothing I can offer to fix it apart from telling you to read the error. They always say where the error is in the output using a ^. Generally, people that get this issue have incorrect indentation, brackets in the wrong place or something spelt wrong. You can read about SyntaxError on Python’s docs here.
‘python’ is not recognized as an internal or external command
Python hasn’t been installed or it hasn’t been installed properly. Go to /blog/post/how-to-setup-pythons-pip/ and follow the tutorial. Just before you enter the scripts folder into the path variable, remove the «\scripts\» part at the end. You will also want to add another path with «\scripts\» to have pip.
Edited 11/08/18: Added Python 2 support
Owner of PyTutorials and creator of auto-py-to-exe. I enjoy making quick tutorials for people new to particular topics in Python and tools that help fix small things.
How to make 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.
You need to press key i to start auto clicker and t to stop auto clicker.
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.