将击键存储到文本文件中

Storing keystrokes into text file

我从 https://pynput.readthedocs.io/en/latest/keyboard.html 中提取了一段代码并对其进行了修改,以便将击键存储到文本文件中。但是,我在输出中收到错误消息:

ImportError: cannot import name 'keyboard' from 'pynput'

谷歌搜索显示...

from pynput.keyboard import Key, Listener

...但即使是此导入也与我的代码不兼容。我已经为 python3 下载了 pynput。原代码来自上面link:

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()

修改后的代码:

import pynput
from pynput import keyboard

def on_press(key):
    with open("keylogger.txt", "a") as f:
        try:
            f.print('alphanumeric key {0} pressed'.format(key.char))
        except AttributeError:
            f.print('special key {0} pressed'.format(key))
def on_release(key):
    with open("keylogger.txt", "a") as f:
        f.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()

我是否遗漏了一些可以让程序将击键正确存储到文本文件中的东西?

第一个不要将您的Python文件命名为模块。

ImportError: cannot import name 'keyboard' from 'pynput' (/root/Testing6/Sample6_2/pynput.py) 

在这里,Python 正在从 您的 pynput 脚本中寻找 keyboard,而不是 actual pynput模块。
Module Search Path:

When a module named spam is imported, the interpreter first searches for a built-in module with that name. If not found, it then searches for a file named spam.py in a list of directories given by the variable sys.path. sys.path is initialized from these locations:

  • The directory containing the input script (or the current directory when no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

这里,Python在当前目录下发现了一个pynput模块,你的脚本,显然不是你要的,确实没有keyboard模块.因此,您只需将其重命名为其他名称(例如 mykeylogger.py)。您当前的导入代码应该可以正常工作:

from pynput import keyboard

第2,没有f.print。 FIle 对象没有 print 方法。
将所有 f.print 更改为 f.write.

def on_press(key):
    with open("keylogger.txt", "a") as f:
        try:
            f.write('alphanumeric key {0} pressed'.format(key.char))
        except AttributeError:
            f.write('special key {0} pressed'.format(key))

Last,请注意 pynput 有一些 platform limitations 可能导致它 not 工作.对于 Linux,您只需确保:

  • An X server must be running.
  • The environment variable $DISPLAY must be set.

之后您的程序运行良好。

$ python3 mykeylogger.py
aaabbb
^CTraceback (most recent call last):
...
KeyboardInterrupt
$ cat keylogger.txt
alphanumeric key a pressed'a' releasedalphanumeric key a pressed'a' releasedalphanumeric key a pressed'a' releasedalphanumeric key b pressed'b' releasedalphanumeric key b pressed'b' releasedalphanumeric key b pressed'b' releasedspecial key Key.enter pressedKey.enter releasedspecial key Key.ctrl pressedalphanumeric key  pressed