views:

43

answers:

1

I'm writing my own simple key logger based on a script I found online. However, I'm trying to write a key command so that the logger program will close when this command is typed. How should I go about this? (Also I know it's not secure at all, however that's not a concern with this program)

For example Ctrl + 'exit' would close the program.

Also It sometimes won't print certain character properly in the .log file it creates, what could be causing this? (I think the character encouding type may be causeing this problem)

#Key Logger
#By: K.B. Carte
#Version 1.0
################

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) + "'")
    return True

hm = pyHook.HookManager()
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()

This is in Windows 7, BTW.

A: 

To get it to close via a certain command, say "quit" ... you'd want to create a buffer .... if you keep everything you log in a buffer, you can easily do

buff += newkeypress
if "quit" in buff[-4:]:
    logfile.close()
    sys.exit(0)

or you can do something like append/pop with a list .. or some other type of circular buffer

for funky characters, you may wind up wanting to just print the whole thing as hex .. or ignoring events less than ascii 0 (such as \b and other funky characters)

Or .. make an ascii table (dictionary) and log the match for the key, so if you get \b you log '' and if you get '0' you log '0'

pyInTheSky
How would I make it Ctrl key then type "quit"?
Anteater7171
try this? http://www.daniweb.com/code/snippet216830.html
pyInTheSky