使自动点击器在按住左键的同时点击

时间:2020-07-23 23:07:25

标签: python python-3.x pynput

正如我所说,我是编码和python的新手,我需要制作一个自动点击器,当我按住鼠标左键时它会切换,而当我释放按钮时会停止。当前代码进行了一个按键绑定,可以将其切换为打开和关闭状态。最近几天我一直在网上搜索,找不到任何东西。预先感谢!

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


x = (0.7, 0.8, 0.9, 0.11)
delay = random.choice(x)
button = Button.left
start_stop_key = KeyCode(char='s')
exit_key = KeyCode(char='e')


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)
            time.sleep(0.1)


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


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


with Listener(on_press=on_press) as listener:
    listener.join()
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()


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


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

1 个答案:

答案 0 :(得分:0)

对于它的价值,我会看一下这种方法: PyAutoGUI Chapter

此处有完整的文档: PyAutoGUI Docs:

相关问题