【发布时间】:2021-05-05 06:09:42
【问题描述】:
即使我松开鼠标左键,while 循环也会返回 true,这会使按下 = false。我不知道如何跳出循环来更新按下的值。
from pynput import keyboard
from pynput import mouse
from pynput.mouse import Button, Controller
control = Controller()
def on_click(x, y, button, pressed):
if button == mouse.Button.left:
while pressed == True:
print(pressed)
with mouse.Listener(
on_click=on_click) as listener:
listener.join()
有什么方法可以更新循环,让它知道什么时候按下 = false。
【问题讨论】:
-
不要使用
while循环,而是正常使用if not pressed。当您运行while循环时,它不能再次运行on_click并且它不能停止这个循环。如果你真的需要在循环中运行一些代码,那么在单独的线程中运行它。
标签: python while-loop mouselistener pynput mouse-listeners