正在尝试使用python制作一个自动clicker

2024-04-26 13:37:51 发布

您现在位置:Python中文网/ 问答频道 /正文

因此,我尝试使用python制作一个自动点击器(我对python还是新手),而自动点击器本身就可以工作。现在我正试图增加点击之间的延迟,并消除点击之间的延迟,但由于某些原因,它不起作用。有人知道为什么吗

import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode

global delay
delay = 0.005
button = Button.left
start_stop_key = KeyCode(char="n")
exit_key = KeyCode(char="e")

up_key = pynput.keyboard.Key[0]
down_key = pynput.keyboard.Key[0]

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)

mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()

def on_press(key):
    delay = 0.005
    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()

    elif key == down_key:
        delay += 0.005

    elif key == up_key:
        delay -= 0.005

with Listener(on_press=on_press) as listener:
    listener.join()

Tags: keyimportselfdefexitbuttonthreadstart