0

i am trying to get keyboard inputs in python on a windows 7 machine. This is my code:

import win32
import win32console
import win32gui
import pythoncom, pyHook

win = win32console.GetConsoleWindow()

def OnKeyBoardEvent(event):
    if (event is None or event.Ascii is None):
        exit(1)
    print(chr(event.Ascii))


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

when i fire up the script in my cmd console the following weird behaviour is happening:

  1. I press any character, e.g. a d => a d appears in the console as well
  2. I press another key, i get this error message:

    Traceback (most recent call last): File "C:\Python34\lib\site-packages\pyHook\HookManager.py", line 351, in Keybo ardSwitch return func(event) File "code.py", line 14, in OnKeyBoardEvent print(event) TypeError: an integer is required (got type NoneType)

    1. I press another key thereafter, both keys (the one from step 2 and step 3) are being displayed.

Thereafter step 2 and 3 are alternating, error message, both characters being shown, error message, both characters being shown etc etc etc.

What is happening here? What am i missing?

Thank you for any help or pointer to the right direction

Tom
  • 906
  • 1
  • 8
  • 30
  • 1
    add `return True` at the end of `OnKeyBoardEvent(event)`. So that it passes to other system handlers. Already resolved here http://stackoverflow.com/q/3049068/6313992 – Tomasz Plaskota Sep 01 '16 at 15:19
  • you should put this in an answer, it works perfectly now, thank you so much! – Tom Sep 01 '16 at 16:57

0 Answers0