0

我的代码的目的是非常快速地连续输入一个字母。它在按下某个键时启动(在本例中为 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 停止程序。此外,该程序应该暂停,而不是退出。任何帮助,将不胜感激。谢谢。

4

1 回答 1

1

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

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

然后你只需要一个Listener来检查密钥并检查是否pausedTrueFalse

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()
于 2021-06-07T17:17:58.433 回答