python - 如何制作左右点击器宏/此代码有什么问题?
问题描述
我正在编辑一些代码(我不太擅长编程),我有两个完美工作的宏(用于右键和左键单击),我正在尝试合并它们,但左键单击器不会启动。右边很好,但左边...
有谁知道这里的问题出在哪里?
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()
解决方案
推荐阅读
- c++ - “扩展初始化列表仅适用于 -std=c++11 或 -std=gnu++11”,我收到此错误
- javascript - 在 React 应用程序中使用 Webpack resolve.alias 进行绝对导入
- wolfram-mathematica - 绘制一个复杂的函数
- google-sheets-api - 使用 Google 电子表格(带有下拉菜单)自动填写 Google 表单
- windows - 是否有用于 Windows 10 即时分辨率更改的 API?
- c++ - 没有显示浮点值?
- c - 写入通过更改指向数组的指针解决的指针时出错。发生了什么?
- xamarin.forms - Mobile login with Oauth - keeping session stored in browser for web views
- visual-studio-code - 在 Visual Studio Code 中调试时是否可以不显示调试侧栏?
- javascript - 'info' 和 'history' 在 tensorflow.js 的模型中未定义