python - 如何在我的自动点击器中添加左右鼠标按钮之间的切换?
问题描述
我最近开始研究我自己的自动点击器应用程序,并且想知道如何添加一个选项来使用热键在鼠标左键和右键之间切换。例如,“l”切换到左侧,而“r”切换到右侧。
这是我的代码:
import time
import threading
from pynput.mouse import Button, Controller
from pynput.keyboard import Listener, KeyCode
delay = 0.001
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()
谢谢
解决方案
推荐阅读
- node.js - 如何使用 vs code 终端使用 npm 命令?
- python - 如何在for循环中跳过1x时间表?
- python - 如何通过将集合作为参数传递来打印字典的键和值?
- javascript - 解构一个对象方法,最后返回 undefined
- javascript - 我可以用一个函数替换所有这些函数吗?
- asp.net - Asp.Net Core 项目发布
- apache-spark - 如何在pySpark中加载一个大文件然后高效处理?
- smtp - 在 Mainframe 中发送电子邮件的不同方式有哪些?
- jquery - 带有是/否按钮事件的引导确认对话框在 Safari 中不起作用
- html - 关闭html页面并返回初始显示