【问题标题】:pyautogui typewrite won't stop typingpyautogui typewrite 不会停止输入
【发布时间】:2019-06-06 00:09:55
【问题描述】:

我正在编写一个脚本,当用户按下 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()

【问题讨论】:

    标签: python loops pyautogui pynput


    【解决方案1】:

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

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

    一种解决方案是创建一个全局的process_keystrokes,并在运行execute() 时将其关闭。清除这组键似乎也是一个好主意,因为我们不知道当打字机发送击键时用户可以/可能做什么。

    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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-30
      • 2020-05-07
      • 2022-07-13
      • 2019-01-02
      • 1970-01-01
      • 2018-12-21
      • 1970-01-01
      相关资源
      最近更新 更多