帮助解决 pyHook 错误 [英] Help with pyHook error

查看:30
本文介绍了帮助解决 pyHook 错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 python 中使用 pyhook 制作一个全局热键,它应该只在按下 alt 键时才起作用.

I'm trying to make a global hotkey with pyhook in python that is supposed to work only with the alt key pressed.

这是来源:

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)...并给出此错误:

but when I execute, only works with the second press of the second key (number 1 = 49)... and give this error:

http://img580.imageshack.us/img580/1858/errord.png

我该如何解决?在第一时间工作.

How can I solve it? For work at the first pressed time.

推荐答案

教程 您需要在处理程序的末尾返回值:

Note from the tutorial that you need a return value at the end of your handler:

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)都会阻塞事件,以便后续处理程序不会得到它.(这让您可以有条件地吞下某些击键,如本教程的事件过滤部分.)

I agree it's ambiguous from the docs whether that's required, but you do need to return True or False (or possibly any integer value), with any "false" value (e.g. 0) blocking the event such that no subsequent handlers get it. (This lets you swallow certain keystrokes conditionally, as in the Event Filtering section of the tutorial.)

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

(This wasn't as easy to figure out as it might look! :-) )

这篇关于帮助解决 pyHook 错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆