Script in Python crashed when alt+tab is pressed repeatedly (pyhook and pythoncom)

400 views Asked by At

I'm making a script in Python 3.5.2 on windows 10 that capture the keys that user press.The code works perfectly except when I press alt+tab to switch the windows, sometimes crashes immediately and sometimes crashes when I press it repeatedly.

The most basic code is next:

import pythoncom, pyHook
def OnKeyboardEvent(event):
    print ('Ascii:', event.Ascii, chr(event.Ascii))
    return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all keyboard events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
#wait forever
pythoncom.PumpMessages()

There is a video with the poblem.

I don't want to stop the script at any circumstance. How can I fix it?

Thanks.

0

There are 0 answers