Skip to content Skip to sidebar Skip to footer

Read Hid Input While Window Is "out Of Focus" In Python

I'm struggling with an issue for some days and I can't get it working. I've just started with python and I'm now already facing the biggest problem which I will face in this projec

Solution 1:

If the barcode scanner presents itself as a keyboard, isn't what you want basically a key logger that runs in the background? Searching for it, this was among the first google results for "python keylogger" - according to the source, it needs pyWin32 and pyHook. I removed the logging to reduce the code sample to a minimum, just put the handling code inOnKeyboardEvent. I tested this and it works with my Python 2.7 installation on Windows 7, but the modules should be compatible with Python 3.3.

import pythoncom, pyHook, sys, logging

def OnKeyboardEvent(event):
    print "Key: ", chr(event.Ascii)
    logging.log(10,chr(event.Ascii))
    return True

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

Post a Comment for "Read Hid Input While Window Is "out Of Focus" In Python"