📜  如何制作Python自动点击器?

📅  最后修改于: 2022-05-13 01:55:14.274000             🧑  作者: Mango

如何制作Python自动点击器?

在本文中,我们将看到如何使用Python创建自动点击器。当用户单击开始键时,代码将从键盘获取输入,并在用户单击退出键时终止自动点击器,自动点击器开始点击指针放在屏幕上的任何位置。我们将在这里使用pynput模块。

什么是自动点击器?

Auto-Clicker 是一个脚本,您可以根据需要多次自动控制鼠标和键盘。它使用用户定义的键进行控制。它适用于各种平台,如 Windows、Mac 和 Linux。 pywin32 模块中存在自动点击器。

方法:

在这个项目中,我们将使用一个跨平台的模块pynput同时控制鼠标和监控键盘来创建简单的自动点击器。为了检查鼠标事件,我们将为此执行安装pynput 模块(用于控制鼠标),在 cmd 中pip install pynput

注意:如果您对如何在系统上设置 python-pip 包感到困惑,请单击此处

安装pynput模块

为此验证pynput模块是否已成功安装到您的工作环境中,在cmdPython Shell的系统上打开IDLE。执行命令import pynput,执行此命令后,输出应该给出零错误,这意味着您的模块已成功安装。

验证模块安装

执行:

现在让我们继续使用Python构建自动点击器所需的代码。按照以下步骤创建自动点击器:

第 1 步:导入时间和线程,然后从pynput.mouse模块导入 Button 和 Controller。从pynput.keyboard导入ListenerKeyCode

Python3
# importing time and threading
import time
import threading
from pynput.mouse import Button, Controller
  
# pynput.keyboard is used to watch events of
# keyboard for start and stop of auto-clicker
from pynput.keyboard import Listener, KeyCode


Python3
# four variables are created to
# control the auto-clicker
delay = 0.001
button = Button.right
start_stop_key = KeyCode(char='a')
stop_key = KeyCode(char='b')


Python3
# threading.Thread is used 
# to control clicks
class ClickMouse(threading.Thread):
    
  # delay and button is passed in class 
  # to check execution of auto-clicker
    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True


Python3
def start_clicking(self):
        self.running = True
  
def stop_clicking(self):
        self.running = False
  
def exit(self):
        self.stop_clicking()
        self.program_running = False


Python3
# method to check and run loop until 
# it is true another loop will check 
# if it is set to true or not, 
# for mouse click it set to button 
# and delay.
def run(self):
    while self.program_running:
        while self.running:
            mouse.click(self.button)
            time.sleep(self.delay)
        time.sleep(0.1)


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


Python3
# on_press method takes
# key as argument
  
  
def on_press(key):
  
  # start_stop_key will stop clicking
  # if running flag is set to true
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
  
    # here exit method is called and when
    # key is pressed it terminates auto clicker
    elif key == stop_key:
        click_thread.exit()
        listener.stop()
  
  
with Listener(on_press=on_press) as listener:
    listener.join()


Python3
# importing time and threading
import time
import threading
from pynput.mouse import Button, Controller
  
# pynput.keyboard is used to watch events of 
# keyboard for start and stop of auto-clicker
from pynput.keyboard import Listener, KeyCode
  
  
# four variables are created to 
# control the auto-clicker
delay = 0.001
button = Button.right
start_stop_key = KeyCode(char='a')
stop_key = KeyCode(char='b')
  
# threading.Thread is used 
# to control clicks
class ClickMouse(threading.Thread):
    
  # delay and button is passed in class 
  # to check execution of auto-clicker
    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
  
    # method to check and run loop until 
    # it is true another loop will check 
    # if it is set to true or not, 
    # for mouse click it set to button 
    # and delay.
    def run(self):
        while self.program_running:
            while self.running:
                mouse.click(self.button)
                time.sleep(self.delay)
            time.sleep(0.1)
  
  
# instance of mouse controller is created
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()
  
  
# on_press method takes 
# key as argument
def on_press(key):
    
  # start_stop_key will stop clicking 
  # if running flag is set to true
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
              
    # here exit method is called and when 
    # key is pressed it terminates auto clicker
    elif key == stop_key:
        click_thread.exit()
        listener.stop()
  
  
with Listener(on_press=on_press) as listener:
    listener.join()


第 2 步:创建如下所述的四个变量,

  • delay : 每次点击之间的延迟(以秒为单位)
  • button : Button 用于单击您想要的任何方向。按钮.left | Button.middle | Button.right
  • start_stop_key :当您运行执行自动点击器的程序时,用于开始和停止点击的键。它应该来自一个关键类或使用KeyCode设置。
  • exit_key :用于终止正在执行的自动点击器的键。这应该来自密钥类或使用KeyCode设置。

蟒蛇3

# four variables are created to
# control the auto-clicker
delay = 0.001
button = Button.right
start_stop_key = KeyCode(char='a')
stop_key = KeyCode(char='b')

第 3 步:创建一个扩展threading.Thread 的类。将延迟和按钮传递给具有两个标志的类来检查程序是否被执行。

蟒蛇3

# threading.Thread is used 
# to control clicks
class ClickMouse(threading.Thread):
    
  # delay and button is passed in class 
  # to check execution of auto-clicker
    def __init__(self, delay, button):
        super(ClickMouse, self).__init__()
        self.delay = delay
        self.button = button
        self.running = False
        self.program_running = True

第 4 步:添加方法以从外部控制线程。

蟒蛇3

def start_clicking(self):
        self.running = True
  
def stop_clicking(self):
        self.running = False
  
def exit(self):
        self.stop_clicking()
        self.program_running = False

第 5 步:在线程启动时创建一个方法, program_running循环运行,直到该值变为真,并且还在现有循环中创建另一个循环,在其中检查running是否设置为 true。如果我们在两个循环中,它将单击设置按钮并在设置的延迟时间内休眠。

蟒蛇3

# method to check and run loop until 
# it is true another loop will check 
# if it is set to true or not, 
# for mouse click it set to button 
# and delay.
def run(self):
    while self.program_running:
        while self.running:
            mouse.click(self.button)
            time.sleep(self.delay)
        time.sleep(0.1)

第六步:为鼠标控制器创建一个实例,然后创建ClickMouse 线程。启动实例以进入 run 方法内的循环。

蟒蛇3

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

第 7 步:创建一个on_press方法,该方法将一个键作为参数并设置一个键盘侦听器。 start_stop_key在执行时与开始键 (a)匹配。然后当线程中的运行标志设置为 True 时,单击将终止。如果执行退出键 (b)并停止侦听器,则在方法中调用 Exit 方法。

蟒蛇3

# on_press method takes
# key as argument
  
  
def on_press(key):
  
  # start_stop_key will stop clicking
  # if running flag is set to true
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
  
    # here exit method is called and when
    # key is pressed it terminates auto clicker
    elif key == stop_key:
        click_thread.exit()
        listener.stop()
  
  
with Listener(on_press=on_press) as listener:
    listener.join()

代码运行后,我们可以在输出中看到如下所示,它显示了代码实现后自动点击器的点击次数。它与 Windows、Mac 和 Linux 兼容。 Auto-Clicker 是对系统有用的软件,因为它可以让我们节省花费在重复点击量上的合理时间。

下面是完整的程序:

蟒蛇3

# importing time and threading
import time
import threading
from pynput.mouse import Button, Controller
  
# pynput.keyboard is used to watch events of 
# keyboard for start and stop of auto-clicker
from pynput.keyboard import Listener, KeyCode
  
  
# four variables are created to 
# control the auto-clicker
delay = 0.001
button = Button.right
start_stop_key = KeyCode(char='a')
stop_key = KeyCode(char='b')
  
# threading.Thread is used 
# to control clicks
class ClickMouse(threading.Thread):
    
  # delay and button is passed in class 
  # to check execution of auto-clicker
    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
  
    # method to check and run loop until 
    # it is true another loop will check 
    # if it is set to true or not, 
    # for mouse click it set to button 
    # and delay.
    def run(self):
        while self.program_running:
            while self.running:
                mouse.click(self.button)
                time.sleep(self.delay)
            time.sleep(0.1)
  
  
# instance of mouse controller is created
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()
  
  
# on_press method takes 
# key as argument
def on_press(key):
    
  # start_stop_key will stop clicking 
  # if running flag is set to true
    if key == start_stop_key:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
              
    # here exit method is called and when 
    # key is pressed it terminates auto clicker
    elif key == stop_key:
        click_thread.exit()
        listener.stop()
  
  
with Listener(on_press=on_press) as listener:
    listener.join()

现在让我们执行我们编写的Python程序,然后按开始 (a)停止 (a)键以启动自动点击器。

输出: