python - Pynput mac 计算机未检测到密钥的释放
问题描述
我有以下代码:
# importing time and threading
import time
import threading
from pynput.mouse import Button, Controller
import random
# pynput.keyboard is used to watch events of
# keyboard for start and stop of auto-clicker
from pynput.keyboard import KeyCode
from pynput.keyboard import Listener as KeyListener
from pynput.mouse import Listener as MouseListener
# four variables are created to
# control the auto-clicker
delay = [20,30] # average cps for delay of [a,b] is a+b-10
button = Button.left
stop_key = KeyCode(char=']')
# threading.Thread is used
# to control clicks
class ClickMouse(threading.Thread):
# delay and button is passed in class
# to check execution of auto-clicker
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
# method to check and run loop until
# it is true another loop will check
# if it is set to true or not,
# for mouse click it set to button
# and delay.
def run(self):
while self.program_running:
p=time.time()
k=1/random.randrange(delay[0],delay[1])
v=(delay[0]+delay[1])/2
while self.running:
if time.time()-p>=0.5:
p=time.time()
k=1/random.randrange(delay[0],delay[1])
mouse.click(self.button)
time.sleep(k)
time.sleep(0.1)
# instance of mouse controller is created
mouse = Controller()
click_thread = ClickMouse(delay, button)
click_thread.start()
# on_press method takes
# key as argument
def on_press(key):
if key == stop_key:
click_thread.exit()
keyListener.stop()
mouseListener.stop()
isPressed=0
mBut=button
def on_click(x, y, button, pressed):
global isPressed
if (button==mBut and pressed):
isPressed=isPressed+1
if (click_thread.running):
pass
else:
#click_thread.start_clicking()
pass
elif (button==mBut and not pressed):
print("rel: "+str(isPressed))
isPressed=isPressed-1
if isPressed<=0:
isPressed=0
click_thread.stop_clicking()
# Collect events until released
mouseListener=MouseListener(on_click=on_click)
print("DONT PUT MOUSE ON IDE")
print(f"Starting in 3 seconds, press {stop_key} to stop.")
time.sleep(3)
mouseListener.start()
keyListener=KeyListener(on_press=on_press)
keyListener.start()
keyListener.join()
当我在 Windows 上按住左键单击时,它会像预期的那样不断地左键单击。但是,当我在 mac 上使用这个程序时,它无法检测到我的触摸板的释放并不断点击。我尝试降低 cps 的速度,但是当我松开鼠标时计算机没有检测到。有什么解决办法吗?也许要使用另一个模块?
解决方案
推荐阅读
- javascript - 带有增强型媒体库(免费或专业版)的 WordPress 5.3 更新导致媒体库文件上传器由于 JavaScript 错误而中断
- python - 如何在 Google 应用引擎上部署 Celery worker
- android - Null 不能转换为非 null 类型 android.widget.RelativeLayout
- python - 如何为直方图选择 x 和 y 轴的值?
- android - 仅从 Android O (--min-api 26) 开始支持调用自定义
- node.js - 猫鼬“更新”不起作用,除非我使用“.then”
- c# - 在给定的示例中,Action Delegate 指的是谁?
- reactjs - 应用快速过滤器并更改行数据后取消选择所有 ag-grid 行
- python - scrapy 出了点问题
- python - 查看复杂对象的内存分布