我想用 pynput 检测键盘,所以我从他们的网站复制并粘贴了一个演示。这里是:
from pynput import keyboard
def on_press(key):
try:
print('alphanumeric key {0} pressed'.format(
key.char))
except AttributeError:
print('special key {0} pressed'.format(
key))
def on_release(key):
print('{0} released'.format(
key))
if key == keyboard.Key.esc:
# Stop listener
return False
# Collect events until released
with keyboard.Listener(
on_press=on_press,
on_release=on_release) as listener:
listener.join()
但是我收到此错误,文件的名称与模块的名称不同:
Traceback (most recent call last):
File "C:\Users\Andrey\Documents\Python\123.py", line 1, in <module>
from pynput import keyboardFile
"C:\Users\Andrey\AppData\Local\Programs\Python\Python36-32\pynput.py", line 10, in <module>
with keyboard.Listener(NameError: name 'keyboard' is not defined