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

喜欢而已 提交于 2019-12-22 12:29:29

问题


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.

来源:https://stackoverflow.com/questions/41203706/script-in-python-crashed-when-alttab-is-pressed-repeatedly-pyhook-and-pythonco

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!