我正在尝试用python中的pyhook创建一个全局热键,它应该只在按下alt键的情况下才能工作。
以下是源代码:
import pyHook
import pythoncom
hm = pyHook.HookManager()
def OnKeyboardEvent(event):
if event.Alt == 32 and event.KeyID == 49:
print 'HERE WILL BE THE CODE'
hm.KeyDown = OnKeyboardEvent
hm.HookKeyboard()
pythoncom.PumpMessages()但当我执行时,只在第二次按下第二个键(数字1=49)时才起作用……并给出这个错误:
http://img580.imageshack.us/img580/1858/errord.png
我该怎么解决它呢?在第一次加班时工作。
发布于 2010-06-16 10:40:35
请注意,在tutorial中,您需要在处理程序的末尾返回一个值:
def OnKeyboardEvent(event):
if event.Alt == 32 and event.KeyID == 49:
print 'HERE WILL BE THE CODE'
# return True to pass the event to other handlers
return True我同意在文档中这是否是必需的模棱两可,但您确实需要返回True或False (或者可能是任何整数值),以及阻塞事件的任何"false“值(例如0),这样后续的处理程序就不会得到它。(这使您可以有条件地接受某些击键,如本教程的事件过滤部分所述。)
(这并不像看起来那么容易理解!:-)
https://stackoverflow.com/questions/3049068
复制相似问题