如何解决Pynput-在键盘和鼠标侦听器中运行 while 循环
该程序的重点是检测何时同时按住 ctrl 键和鼠标左键。当我运行它时,即使我释放了 ctrl 键,detect
函数仍然在运行。 on_press
似乎不会更新 ctrl 键的布尔值,detect
仍会在仅按住鼠标左键的情况下运行。
import threading
from pynput import mouse
from pynput import keyboard
mouse_running = False
keyboard_running = False
coordList = [(2,3),(1,7),(4,8)]
def detected():
while keyboard_running and mouse_running:
for i in coordList:
move_point = coordList[i]
#move_point is now a tuple from the coordList. ex: (2,3)
pointer.move(move_point[0],move_point[1])
def on_click(x,y,button,pressed):
global mouse_running
if button == mouse.Button.left:
if pressed:
if not mouse_running:
mouse_running = True
threading.Thread(target=detected).start()
else:
mouse_running = False
def on_press(key):
global keyboard_running
if key == keyboard.Key.ctrl_l:
if not keyboard_running:
keyboard_running = True
threading.Thread(target=detected).start()
else:
keyboard_running = False
Mouse_Listener = mouse.Listener(on_click=on_click)
Keyboard_Listener = keyboard.Listener(on_press=on_press)
Mouse_Listener.start()
Keyboard_Listener.start()
Keyboard_Listener.join()
Mouse_Listener.join()
在按一次 ctrl 键后 lmb 将继续运行 detect
。如果同时按住 lmb 和 ctrl,我有什么办法可以让 detect
运行。
这是程序的带注释的控制台结果。左边是keyboard_running
的值,右边是mouse_running
的值。
#ctrl pressed and released,left mouse pressed once
True True
False True
False True
False True
False True
False True
False True
#False True continues about 30 times.
非常感谢
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。