pyautogui typewrite不会停止打字

pyautogui typewrite won't stop typing

我正在编写一个脚本,当用户按下 Shift+P 时,将输入一个文本字符串。它有效,当我按下 Shift+P 时,它输入了文本,但它并没有停止输入文本。我认为这是我做过但没有看到的事情。为什么这会一直循环和打字?如何让它在输入完 "Hello, World" 一次后停止?

from pynput import keyboard
import pyautogui as pg

COMBINATIONS = [
        {keyboard.Key.shift, keyboard.KeyCode(char="p")},
        {keyboard.Key.shift, keyboard.KeyCode(char="P")}
        ]

current = set()

def execute():
    pg.press("backspace")
    pg.typewrite("Hello, World\n", 0.25)

def on_press(key):
    if any ([key in COMBO for COMBO in COMBINATIONS]):
        current.add(key)
        if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
            execute()

def on_release(key):
    if any([key in COMBO for COMBO in COMBINATIONS]):
        current.remove(key)

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

这里发生的事情很棘手。 py.typewrite 调用 on_press 信号处理程序。但它不只是调用它...它用 keyboard.Key.Shift 调用 on_press,因为 Hello World![= 中的大写 H (shift-h) 和感叹号 (shift-1) 13=]

首先,如果发送 hello world 和不发送 Hello World!,你可以看到区别 对于小写版本,打字机从不发送 shift 键,所以我们不 运行 on_press 它不认为,哦,我们有 p 并向下移动,我们需要在完成后再次 运行 Hello World。

一个解决方案是制作一个全局的,process_keystrokes,并在 运行ning execute() 时将其关闭。清除键组似乎也是个好主意,因为我们不知道用户 can/may 在打字机发送击键时做了什么。

from pynput import keyboard
import pyautogui as pg

COMBINATIONS = [
        {keyboard.Key.shift, keyboard.KeyCode(char="p")},
        {keyboard.Key.shift, keyboard.KeyCode(char="P")}
        ]

current = set()

pg.FAILSAFE = True # it is by default, but just to note we can go to the very upper left to stop the code

process_keystrokes = True

def execute():
    global process_keystrokes
    process_keystrokes = False # set process_keystrokes to false while saying HELLO WORLD
    pg.press("backspace")
    pg.typewrite("#Hello, World\n", 1)
    process_keystrokes = True

def on_press(key):
    if not process_keystrokes: return
    if any ([key in COMBO for COMBO in COMBINATIONS]):
        current.add(key)
        print("Added", key)
        if any(all(k in current for k in COMBO) for COMBO in COMBINATIONS):
            execute()
            current.clear() # note this may potentially not track if we held the shift key down and hit P again. But unfortunately typewriter can stomp all over the SHIFT, and I don't know what to do. There seems to be no way to tell if the user let go of the shift key, so it seems safest to clear all possible keystrokes.

def on_release(key):
    if not process_keystrokes: return
    if any([key in COMBO for COMBO in COMBINATIONS]):
        print("Removed", key)
        current.remove(key)

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