如何查看刚刚在pynput中按下的键

How to see what key was just pressed in pynput

from pynput import keyboard
from pynput.keyboard import Key, Listener



def on_press(key):
print(f'{key} pressed')


def on_release(key):
print(f'{key} release')
if key == "a":
    play_audio("A0.ogg")
if key == Key.esc:
    return False



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

我试过的是 如果键 == "a": play_audio("A0.ogg")

但是好像不行。 我也在用监听器。

两种方法:

def on_release(key):
    print(f'{key} release')
    if key.char == 'a': # when press some specific keys, this will raise an Exception.
        play_audio("A0.ogg")
    if key == Key.esc:
        return False

或直接使用:

def on_release(key):
    print(f'{key} release')
    if str(key) == 'a':
        play_audio("A0.ogg")
    if key == Key.esc:
        return False