Diode_exe

python autoclicker

May 18th, 2025
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. from pynput.mouse import Button, Controller as MouseController
  2. from pynput import keyboard
  3. import time
  4.  
  5. mouse = MouseController()
  6. clicking = True
  7.  
  8. def on_press(key):
  9. global clicking
  10. try:
  11. if key.char == 'q':
  12. clicking = False
  13. return False # Stop the listener
  14. except AttributeError:
  15. pass
  16.  
  17. # Ask the user how to run the clicker
  18. while True:
  19. readMouse = input("Press 1 to just run, or 2 to print the mouse position and run: ")
  20. if readMouse in {"1", "2"}:
  21. break
  22. print("Invalid selection. Try again.")
  23.  
  24. # Ask how many clicks to do
  25. amountOfClicks = input("Choose the amount of times to click, or press Enter to infinitely click: ")
  26.  
  27. # Convert amountOfClicks to an integer, if provided
  28. clicks = None
  29. if amountOfClicks.strip():
  30. try:
  31. clicks = int(amountOfClicks)
  32. except ValueError:
  33. print("That's not a number! Exiting.")
  34. exit()
  35.  
  36. print("Giving you 5 seconds to switch windows... (Press 'q' anytime to quit later)")
  37. time.sleep(5)
  38.  
  39. def click_loop(print_position=False, max_clicks=None):
  40. global clicking
  41. count = 0
  42.  
  43. with keyboard.Listener(on_press=on_press):
  44. while clicking:
  45. if print_position:
  46. print(f"Mouse at: {mouse.position}")
  47. mouse.press(Button.left)
  48. mouse.release(Button.left)
  49. time.sleep(0.05)
  50. if max_clicks is not None:
  51. count += 1
  52. if count >= max_clicks:
  53. break
  54.  
  55. # Run based on user input
  56. click_loop(print_position=(readMouse == "2"), max_clicks=clicks)
  57.  
Advertisement
Add Comment
Please, Sign In to add comment