【问题标题】:Help with pyHook error帮助解决 pyHook 错误
【发布时间】:2010-06-15 21:12:08
【问题描述】:

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

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

【问题讨论】:

    标签: python windows


    【解决方案1】:

    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(或者可能是任何整数值),并且任何“假”值(例如 0)都会阻止事件,这样后续处理程序就不会得到它。 (这可以让您有条件地吞下某些击键,如本教程的事件过滤部分。)

    (这并不像看起来那么容易弄清楚!:-))

    【讨论】:

      猜你喜欢
      • 2011-08-21
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多