【问题标题】:loop until key is pressed and repeat循环直到按键被按下并重复
【发布时间】:2021-05-17 16:00:41
【问题描述】:

我试图在按下一个键时循环打印并在按下另一个键时停止。

另外我不想退出程序,它必须继续监听一个键。

问题:但我得到的是一个无限循环,因为它无法在循环为 True 时监听另一个键!

from pynput import keyboard
    
def on_press(key):
    if key == keyboard.Key.f9:
        while True:
            print("loading")
    if key == keyboard.Key.f10:
        # Stop listener
        return False

# Collect events until released
with keyboard.Listener(on_press=on_press) as listener:
    listener.join()

Im using pynput, docs here

编辑

我确实解释错了一部分,我想在按下某个键WAS时循环。

【问题讨论】:

  • 不要使用while True,因为它会阻止Listener。您必须在分开的Thread 中运行while,它需要一些变量来停止它 - 即。 while running 而不是 while True。而且你必须使用on_release 来设置running = False

标签: python loops while-loop keyboard pynput


【解决方案1】:

您不能在 on_press 内运行 while True(或任何长时间运行的函数)
因为它阻止了Listener。您必须在单独的thread 中运行它。

您需要on_press 来创建和启动thread
on_release 停止thread
它需要全局变量。 IE。 running 这个。

我使用datetime 只是为了查看它是否显示新行。

from pynput import keyboard
import threading
import datetime

# --- functions ---

def loading():
    while running:
         print("loading", datetime.datetime.now()) #, end='\r')
    
def on_press(key):
    global running  # inform function to assign (`=`) to external/global `running` instead of creating local `running`

    if key == keyboard.Key.f9:
        running = True
        # create thread with function `loading`
        t = threading.Thread(target=loading)
        # start thread
        t.start()
        
    if key == keyboard.Key.f10:
        # stop listener
        return False

def on_release(key):
    global running  # inform function to assign (`=`) to external/global `running` instead of creating local `running`
    
    if key == keyboard.Key.f9:
        # to stop loop in thread
        running = False
        
#--- main ---

with keyboard.Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

【讨论】:

  • 您不必将我的解决方案放在您的问题中。您将我的回答标记为已接受,因此每个人都知道我的回答解决了您的问题。如果您使用我的代码为您的问题创建更好的解决方案,然后将您的代码作为另一个答案并添加即based on @furas code
【解决方案2】:

按键按下时的解决方案。 based on @furas code

from pynput import keyboard
import threading
import datetime, time

# --- functions ---

def loading():
    while running:
        print("loading", datetime.datetime.now()) #, end='\r')
        time.sleep(1)
    
def on_press(key):
    global running  # inform function to assign (`=`) to external/global `running` instead of creating local `running`

    if key == keyboard.Key.f9:
        running = True
        # create thread with function `loading`
        t = threading.Thread(target=loading)
        # start thread
        t.start()
        
    if key == keyboard.Key.f10:
        # to stop loop in thread
        running = False

    if key == keyboard.Key.f11:
        # stop listener
        return False
        
#--- main ---

with keyboard.Listener(on_press=on_press) as listener:
    listener.join()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 2022-12-31
    • 2021-11-12
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多