Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Instagram Bot using Python and InstaPy
Next article icon

Auto-clickersare tools that simulate mouse clicks automatically at a given location and interval. Whether you're automating repetitive tasks in games, productivity applications, or testing graphical user interfaces (GUIs), creating an auto-clicker in Python is both a fun and practical project. In this article, we’ll walk you through building a fully functional auto-clicker using the pynput module to monitor keyboard input and simulate mouse clicks.

Requirements

We’ll be using the pynput module, a cross-platform library for controlling and monitoring input devices (mouse/keyboard). To install pynput, run the following command in your terminal or command prompt:

pip install pynput

Installation of pynput module

Note:If you're stuck on how to set up python-pip package on your system thenclick here

After installing this module, we need to verify whether it has been installed correctly. Open your Python shell or IDLE and type:

Verifying module installation

Step by step implementaion

Let's now proceed with the code that is required to build anAuto-clickerusing Python. Follow the below steps to create an auto-clicker:

Step 1:Start by importing the necessary modules. These allow us to work with time delays, threads for background execution and to control keyboard and mouse inputs.

Python
importtimeimportthreadingfrompynput.mouseimportButton,Controllerfrompynput.keyboardimportListener,KeyCode


Step 2:We set up key variables that determine how the clicker behaves:

  • d: Time gap between each click.
  • btn:Which mouse button to click.
  • start_key:Hotkey to start or stop clicking.
  • exit_key:Hotkey to exit the program.
Python
# Config variablesd=0.001btn=Button.left# Hotkeysstart_key=KeyCode(char='a')exit_key=KeyCode(char='b')


Step 3:We use Python’sthreading.Thread to run the auto-clicker in the background so it doesn't block keyboard input.

Python
classAutoClicker(threading.Thread):def__init__(self,d,btn):super().__init__()self.d=d# Delayself.btn=btn# Buttonself.clicking=Falseself.active=Truedefstart_click(self):self.clicking=Truedefstop_click(self):self.clicking=Falsedefexit(self):self.stop_click()self.active=Falsedefrun(self):whileself.active:whileself.clicking:mouse.click(self.btn)time.sleep(self.d)


Step 4: We create a Controller instance to simulate mouse actions, and then start the thread we just defined.

Python
mouse=Controller()c=ClickMouse(d,btn)c.start()


Step 5:This function listens for key presses. If the user presses a, the clicker toggles on or off. Pressing b exits the application.

Python
defon_press(k):ifk==start_key:ifclicker.clicking:clicker.stop_click()print("[INFO] Clicker Stopped.")else:clicker.start_click()print("[INFO] Clicker Started.")elifk==exit_key:clicker.exit()print("[INFO] Exiting.")returnFalse# Stop listener


Step 6:Creating an instance for the mouse controller, then createClickMouse thread. Start the instance to move into the loop inside the run method.

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


Step 7:Now we start the keyboard listener, which keeps the program running and listens for hotkey presses.

Python
withListener(on_press=on_press)aslistener:listener.join()

Full Source code

Python
importtimeimportthreadingfrompynput.mouseimportButton,Controllerfrompynput.keyboardimportListener,KeyCode# Config variablesd=0.001# Delay between clicksbtn=Button.left# Mouse button to click# Hotkeysstart_key=KeyCode(char='a')# Start/stop keyexit_key=KeyCode(char='b')# Exit keyclassAutoClicker(threading.Thread):def__init__(self,d,btn):super().__init__()self.d=dself.btn=btnself.clicking=Falseself.active=Truedefstart_click(self):self.clicking=Truedefstop_click(self):self.clicking=Falsedefexit(self):self.stop_click()self.active=Falsedefrun(self):whileself.active:whileself.clicking:mouse.click(self.btn)time.sleep(self.d)# Create mouse controllermouse=Controller()# Create and start the auto-clicker threadclicker=AutoClicker(d,btn)clicker.start()# Keyboard listener functiondefon_press(k):ifk==start_key:ifclicker.clicking:clicker.stop_click()print("[INFO] Clicker Stopped.")else:clicker.start_click()print("[INFO] Clicker Started.")elifk==exit_key:clicker.exit()print("[INFO] Exiting.")returnFalse# Stop listener# Start listening for keyboard eventswithListener(on_press=on_press)aslistener:listener.join()

Now let's execute the python program we've written and then press thestart (a) andstop (a) keys in order to initiate the auto clicker. 

Output:


How to make a Python auto clicker?
Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp