在MacOS Mojave和python 2.7.15上,此代码:
from pynput import keyboard
def on_press(key):
print('Key {} pressed.'.format(key))
def on_release(key):
print('Key {} released.'.format(key))
if str(key) == 'Key.esc':
print('Exiting...')
return False
with keyboard.Listener(
on_press = on_press,
on_release = on_release) as listener:
listener.join()
依次按“ h j sscape”键时会产生此输出
hKey 'h' pressed.
Key 'h' released.
Key 'j' pressed.
jKey 'j' released.
Key 's' pressed.
sKey 's' released.
Key Key.esc pressed.
^[Key Key.esc released.
Exiting...
%hjs
我不明白为什么
1)字母'h'在on_press函数的输出之前
2)字母'j','s','^ [Key'在on_release函数的输出之前
3)程序执行终止后,在终端提示符后出现字母“ hjs”
我有一种感觉,键盘输入被pynput拦截,但随后pynput在程序执行期间将其再次回显到终端。可以抑制吗?但是程序结束后,会再次出现字母。这些是从哪里来的,我该如何抑制这种行为?
我正在尝试添加非阻塞式键盘输入,以退出具有循环不断运行的程序的