如何在 pynput 中跟踪字母数字字符和非特殊字符(空格键,删除,制表符等)。我使用以下代码。此代码仅输出特殊字符,而不输出通过键盘输入的任何其他字符。
from pynput.keyboard import Key, Listener
#defining function to print when key is pressed
def on_press(key):
print('{0} pressed'.format(key))
#defining function to print when key is released
def on_release(key):
print('{0} release'.format(key))
if key == Key.esc:
# Stop listener
return False
# Collect events until released
with Listener(on_press=on_press, on_release=on_release) as listener:
listener.join()