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:
- I press any character, e.g. a d => a d appears in the console as well
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)
- 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