【问题标题】:Exit loop with pynput input使用 pynput 输入退出循环
【发布时间】:2021-08-24 18:30:30
【问题描述】:

我的代码的目的是非常快速地连续输入一个字母。它在按下某个键时启动(在本例中为 f3),并在按下另一个键时停止(f4)。我的代码目前看起来像这样。

from pynput.keyboard import Controller, Listener
import time

keyboard = Controller()
confirm = False


def on_press(key):
    if "f3" in str(key):
        global confirm
        confirm = True
        while confirm:
            keyboard.press('e')
            keyboard.release('e')
            time.sleep(0.10)
    elif "Key." in str(key):
        pass


def exit_loop(key):
    if "f4" in str(key):
        global confirm
        confirm = False
    elif "Key." in str(key):
        pass


with Listener(on_press=on_press) as ListenerStart:
    ListenerStart.join()

with Listener(on_press=exit_loop) as ListenerEnd:
    ListenerEnd.join()

我的问题是,当使用 f3 键启动程序时,我无法使用 f4 停止程序。此外,该程序应该暂停,而不是退出。任何帮助,将不胜感激。谢谢。

【问题讨论】:

  • 你运行 while-loop 会阻塞所有代码 - 你应该在单独的线程中运行它。
  • 你应该使用一个带有on_press的监听器来检查按下了什么键并设置一些变量,如started = True、paused = False等,它应该使用这些变量来运行或跳过里面的代码while-loop 应该在单独的线程中运行。

标签: python pynput


【解决方案1】:

如果您有像 while-loop 这样长时间运行的代码,那么您必须在单独的线程中运行它 - 因为它会阻塞当前代码并且无法检查您是否按下 f4。

如果你想暂停代码,那么你应该使用一些变量 - 即。 paused = True - 控制是否应该执行或跳过 while-loop 中的代码。

然后您只需要一个Listener 来检查密钥并检查paused 是True 还是False

from pynput.keyboard import Controller, Listener
import time
import threading

def function():
    keyboard = Controller()
    
    while True:
        if not paused:
            keyboard.press('e')
            keyboard.release('e')
        time.sleep(0.1)

def on_press(key):
    global paused
    
    if paused:
        if "f3" in str(key):
            paused = False
    else:
        if "f4" in str(key):
            paused = True

# global variables with default values at star
paused  = True

# run long-running `function` in separated thread
thread = threading.Thread(target=function)  # function's name without `()`
thread.start()

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

在正确的代码中,您可以使用相同的f3 来启动和暂停循环。

from pynput.keyboard import Controller, Listener
import time
import threading

def function():
    keyboard = Controller()
    
    while True:
        if not paused:
            keyboard.press('e')
            keyboard.release('e')
        time.sleep(0.1)

def on_press(key):
    global paused
    
    if "f3" in str(key):
        paused = not paused

# global variables with default values at star
paused = True

# run long-running `function` in separated thread
thread = threading.Thread(target=function)
thread.start()

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

这段代码可能更复杂 - f3 可以检查 thread 是否已经存在并在它不存在时创建线程。

from pynput.keyboard import Controller, Listener
import time
import threading

def function():
    keyboard = Controller()
    
    while True:
        if not paused:
            keyboard.press('e')
            keyboard.release('e')
        time.sleep(0.1)

def on_press(key):
    global paused
    global thread
    
    if "f3" in str(key):
        paused = not paused
        if thread is None:
            # run long-running `function` in separated thread
            thread = threading.Thread(target=function)
            thread.start()
            
# global variables with default values at star
paused = True
thread = None

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-02
    • 2022-01-18
    • 2019-03-19
    • 1970-01-01
    • 2018-07-25
    • 2014-04-12
    相关资源
    最近更新 更多