简体   繁体   English

按下鼠标按钮后,如何使程序不会无限循环运行

[英]How can I make my program not run in an endless loop once my mouse button is pressed

So I'm trying to make the program not run in an endless loop once pressed is true and check for when the mouse button is released and stop the while loop.所以我试图让程序一旦按下就不会在无限循环中运行,并检查何时释放鼠标按钮并停止 while 循环。

from pynput import mouse
from pynput.mouse import Button, Controller
import time
m = Controller()


def on_click(x, y, button, pressed):
    print('Pressed' if pressed else 'Released')

    while (pressed):
        m.move(0, 1)
        time.sleep(.001)

with mouse.Listener(
        on_click=on_click) as listener:
    listener.join()

Something like this.像这样的东西。 Don't try to sleep for 1ms, you'll waste too many resources.不要尝试休眠 1ms,会浪费太多资源。

from pynput import mouse
from pynput.mouse import Button, Controller
import time
m = Controller()

mousedown = False


def on_click(x, y, button, pressed):
    global mousedown
    print('Pressed' if pressed else 'Released')
    mousedown = pressed

listener = mouse.Listener(on_click=on_click) 

while True:
    if mousedown:
        m.move( 0, 1 )
    time.sleep( 0.1 )

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在我的程序中创建一个循环以使其在 python 中每个用户选项的末尾运行? - How can I create a loop in my program to make it run at the end of each user option in python? 如何使我的if语句偶尔运行一次 - How to make my if statement run once in a while loop 如何在我的 tk UI 中创建一个按钮,使程序在按下时执行大量操作而不冻结 window? - How do I make a button in my tk UI that makes the program do a whole ton of stuff when it is pressed not freeze the window? 我怎样才能让我的 python 程序运行得更快? 我收到以下代码的“killed 9”消息 - How can i make my python program run faster? I get "killed 9" message with following code 如何在不阻塞整个程序的情况下使函数在后台运行? - How can I make a function run in the background without blocking my entire program? 我怎样才能让我的程序根据 key.is_pressed 语句播放声音? - How could i make my program play sounds according to key.is_pressed statement? Python:我怎样才能让我的功能打印一次? - Python: How can I make that my funtion print once? 程序结束后如何自动运行命令? - How do I run automatically run a command once my program ends? 我试图找到快乐的数字,但我的程序进入了无限循环 - I'm trying to find happy numbers but my program goes into an endless loop 如何在 class 中正确制作 discord.Client 并在我的循环中作为任务运行? - How can I make discord.Client in a class correctly and run as a task in my loop?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM