I am attempting to make an autoclicker that checks first if the clicker is enabled then if a user is holding down the left mouse button. If the conditions are met, the program would click with a range of delays. The auto clicking should stop once the user releases the left mouse button. The code I currently have is enabling and disabling fine but when I hold down my mouse button I get 1-2 clicks and then it just stops.
Second issue I have is when using the auto clicker's hotkeys is I have to not be pressing any additional keys to for the hot key to activate. So for example while playing a game I am unable to move around and provide other inputs enableing or disabling the auto clicker.
If anyone has any fixes for either of these issues I would greatly appreciate any input.
import ctypes
import time
import threading
import random
import keyboard
# Constants for Windows API mouse events
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
VK_LBUTTON = 0x01 # Virtual key code for the left mouse button
# Windows API functions
user32 = ctypes.windll.user32
def click():
user32.mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
user32.mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def is_left_mouse_button_pressed():
return user32.GetAsyncKeyState(VK_LBUTTON) & 0x8000 != 0
# Control variables
enabled = False # Toggle for enabling/disabling the clicker
# Hotkeys
ENABLE_KEY = 'g' # Press 'g' to enable/disable auto-clicking
EXIT_KEY = '0' # Press '0' to exit
def clicker():
while True:
if enabled: # Only continue clicking if enabled
while is_left_mouse_button_pressed(): # Check if the left mouse button is pressed
click()
time.sleep(random.uniform(0.05, 0.3)) # Randomized delay between clicks
time.sleep(0.01) # Short delay to prevent high CPU usage when left-click is not held
else:
time.sleep(0.01) # Sleep to prevent high CPU usage when disabled
def toggle_clicking():
global enabled
enabled = not enabled # Toggle state
print(f"Auto-clicker {'enabled' if enabled else 'disabled'}")
# Start clicking thread
clicking_thread = threading.Thread(target=clicker, daemon=True)
clicking_thread.start()
# Set up hotkeys
keyboard.add_hotkey(ENABLE_KEY, toggle_clicking, suppress=False) # Press 'g' to toggle auto-clicking
# Wait for exit key
keyboard.wait(EXIT_KEY) # Press '0' to exit
I am attempting to make an autoclicker that checks first if the clicker is enabled then if a user is holding down the left mouse button. If the conditions are met, the program would click with a range of delays. The auto clicking should stop once the user releases the left mouse button. The code I currently have is enabling and disabling fine but when I hold down my mouse button I get 1-2 clicks and then it just stops.
Second issue I have is when using the auto clicker's hotkeys is I have to not be pressing any additional keys to for the hot key to activate. So for example while playing a game I am unable to move around and provide other inputs enableing or disabling the auto clicker.
If anyone has any fixes for either of these issues I would greatly appreciate any input.
import ctypes
import time
import threading
import random
import keyboard
# Constants for Windows API mouse events
MOUSEEVENTF_LEFTDOWN = 0x0002
MOUSEEVENTF_LEFTUP = 0x0004
VK_LBUTTON = 0x01 # Virtual key code for the left mouse button
# Windows API functions
user32 = ctypes.windll.user32
def click():
user32.mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
user32.mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
def is_left_mouse_button_pressed():
return user32.GetAsyncKeyState(VK_LBUTTON) & 0x8000 != 0
# Control variables
enabled = False # Toggle for enabling/disabling the clicker
# Hotkeys
ENABLE_KEY = 'g' # Press 'g' to enable/disable auto-clicking
EXIT_KEY = '0' # Press '0' to exit
def clicker():
while True:
if enabled: # Only continue clicking if enabled
while is_left_mouse_button_pressed(): # Check if the left mouse button is pressed
click()
time.sleep(random.uniform(0.05, 0.3)) # Randomized delay between clicks
time.sleep(0.01) # Short delay to prevent high CPU usage when left-click is not held
else:
time.sleep(0.01) # Sleep to prevent high CPU usage when disabled
def toggle_clicking():
global enabled
enabled = not enabled # Toggle state
print(f"Auto-clicker {'enabled' if enabled else 'disabled'}")
# Start clicking thread
clicking_thread = threading.Thread(target=clicker, daemon=True)
clicking_thread.start()
# Set up hotkeys
keyboard.add_hotkey(ENABLE_KEY, toggle_clicking, suppress=False) # Press 'g' to toggle auto-clicking
# Wait for exit key
keyboard.wait(EXIT_KEY) # Press '0' to exit
Share
Improve this question
edited Mar 13 at 5:40
user5127
16512 bronze badges
asked Mar 13 at 5:07
MatteoMatteo
133 bronze badges
1
- The user's click might be interfering with the program's click. Maybe try checking if a key is pressed instead – user5127 Commented Mar 13 at 5:25
1 Answer
Reset to default 0Problem is that you attempt to build a self-sustaining solution, where a left mouse click shall induce lots of left mouse clicks. Whenever you produce a user32.mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
, however, your condition is_left_mouse_button_pressed()
gets false and thus exits the while-loop.
Quick fix that works for me is just changing the order of button up and button down events (as the mouse button is already down you leave the click()
-method with the button in the same state).
clicks = 0 # Counter for the number of clicks
def click():
global clicks
clicks += 1
user32.mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
print(f"Click {clicks}")
user32.mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
Another solution is to use a different trigger, e.g. the right mouse button or a key to control the auto clicking.
For the second issue, the problem is that keyboard
is very specific with the hotkeys. To check the state of your hotkey, even with any other modifiers or keys pressed, you can introduce another thread that explicitly listens for those key presses:
def check_key_press():
global enabled
while True:
if user32.GetAsyncKeyState(ord(ENABLE_KEY.upper())) & 0x8000:
enabled = not enabled
print(f"Auto-clicker {'enabled' if enabled else 'disabled'}")
time.sleep(0.5)
Happy gaming!