1

我想用 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
4

1 回答 1

0

您正在导入不同的pynput, file C:\Users\Andrey\AppData\Local\Programs\Python\Python36-32\pynput.py,可能是您自己的。这就是你的错误所在。

将此文件移出 Python 路径。

于 2018-07-18T08:30:32.977 回答