帮助pyHook错误

时间:2010-06-15 21:12:09

标签: python windows

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

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

1 个答案:

答案 0 :(得分:9)

请注意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)阻止事件,以便后续处理程序不会它。 (这使您可以有条件地吞下某些键击,如本教程的“事件过滤”部分所述。)

(这可能不像它看起来那么容易!:-))

相关问题