当键盘侦听器完成时,Pynput 打印输入的键

Mar*_*son 5 python listener termios pynput

这让我很困扰。当使用带有 Keyboard.Listener 的 Python pynput 模块时,键盘监听器停止后,键盘监听时输入的任何内容都会在终端中打印出来。

我尝试过 termios.tcflush(sys.stdout, termios.TCIOFLUSH) 但它似乎没有清除缓冲区并停止将字符打印到终端窗口中。

非常感谢任何帮助!下面的脚本 -


import sys
from pynput import keyboard
import termios
global go_to_selection

def on_press(key):
    global go_to_selection

    if key != keyboard.Key.tab and key != keyboard.Key.shift and key != keyboard.Key.enter:

        termios.tcflush(sys.stdout, termios.TCIOFLUSH)
        go_to_selection = True
        return False


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

if go_to_selection == True:
    _user_choice = input('\r\nSelection: ')
    print('  Chosen: '+ str(_user_choice))
Run Code Online (Sandbox Code Playgroud)

编辑:

with keyboard.Listener(on_press=on_press, suppress=True ) as listener:
        listener.join()
Run Code Online (Sandbox Code Playgroud)

实际上修复了这个问题,但在键盘再次响应之前似乎会导致 1-2 秒的延迟。这是预期的吗?

谢谢!