gpt4 book ai didi

python - 帮助解决 pyHook 错误

转载 作者:可可西里 更新时间:2023-11-01 13:52:50 25 4
gpt4 key购买 nike

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

我该如何解决?为了第一时间的工作。

最佳答案

来自 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)都会阻止事件,这样后续处理程序就不会得到它。 (这使您可以有条件地吞下某些击键,如本教程的事件过滤部分所示。)

(这并不像看起来那么容易理解!:-))

关于python - 帮助解决 pyHook 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3049068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com