帮助与pyHook错误

我正在尝试使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

我该如何解决? 在第一次按下的时间工作。

请注意,本教程中您需要在处理程序结束时返回一个值:

 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(或可能是任何整数值),与任何“假”值(例如0)阻止事件,以便后续处理程序得到它。 (这可以让您有条件地吞咽某些按键,如本教程的“事件筛选”部分所示。)

(这不太容易弄明白,因为它可能看起来!:-))