PyHook无法检测到某些窗口中按下的键

bad*_*rqq 13 python pyhook

我运行了这个PyHook示例代码:

    import pythoncom, pyHook

def OnKeyboardEvent(event):
    print 'MessageName:',event.MessageName
    print 'Message:',event.Message
    print 'Time:',event.Time
    print 'Window:',event.Window
    print 'WindowName:',event.WindowName
    print 'Ascii:', event.Ascii, chr(event.Ascii)
    print 'Key:', event.Key
    print 'KeyID:', event.KeyID
    print 'ScanCode:', event.ScanCode
    print 'Extended:', event.Extended
    print 'Injected:', event.Injected
    print 'Alt', event.Alt
    print 'Transition', event.Transition
    print '---'

# return True to pass the event to other handlers
    return True

# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.KeyDown = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()
Run Code Online (Sandbox Code Playgroud)

要尝试它,它可以在大多数窗口中使用,但是当我在游戏窗口中尝试它时,就像我没有按任何按钮一样。

有没有办法听特定过程中的按键?我该怎么办?

Abh*_*yap 2

我引用的是您获得此代码的同一页面。 检查这里。

如果您使用的是 GUI 工具包(例如 wxPython),则不需要此循环,因为该工具包提供了自己的循环。

我认为你应该换pythoncom.PumpMessages()一些我不知道是什么的东西。