首页 > 解决方案 > 如何制作左右点击器宏/此代码有什么问题?

问题描述

我正在编辑一些代码(我不太擅长编程),我有两个完美工作的宏(用于右键和左键单击),我正在尝试合并它们,但左键单击器不会启动。右边很好,但左边...

有谁知道这里的问题出在哪里?

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


delay = 0.0001
buttonR = Button.right
start_stop_keyR = KeyCode(char='+')
exit_keyR = KeyCode(char='+')
delay = 0.0001
buttonL = Button.left
start_stop_keyL = KeyCode(char='-')
exit_keyL = KeyCode(char='-')



class ClickMouseR(threading.Thread):
    def __init__(self, delay, button):
        super(ClickMouseR, self).__init__()
        self.delay = delay
        self.button = buttonR
        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 = ClickMouseR(delay, buttonR)
click_thread.start()


def on_press(key):
    if key == start_stop_keyR:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
    elif key == exit_keyR:
        click_thread.exit()
        listener.stop()


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

    class ClickMouseL(threading.Thread):
        def __init__(self, delay, buttonL):
            super(ClickMouseL, self).__init__()
            self.delay = delay
            self.button = buttonL
            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 = ClickMouseL(delay, buttonL)
click_thread.start()


def on_press(key):
    if key == start_stop_keyL:
        if click_thread.running:
            click_thread.stop_clicking()
        else:
            click_thread.start_clicking()
    elif key == exit_keyL:
        click_thread.exit()
        listener.stop()


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

标签: pythonmousepynput

解决方案


推荐阅读