python - PyHook 未检测到某些窗口中按下的键

标签 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()

尝试一下,它在大多数窗口中都有效,但是当我在游戏窗口中尝试时,它就像我没有按任何东西一样。

有没有办法监听来自特定进程的按键?我该怎么办?

最佳答案

我是从您获得此代码的同一页面上引用的。 Check Here.

If you are using a GUI toolkit (e.g. wxPython), this loop is unnecessary since the toolkit provides its own.

我认为您应该将 pythoncom.PumpMessages() 更改为我不知道是什么的其他内容。

关于python - PyHook 未检测到某些窗口中按下的键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33377775/

相关文章:

python 3 : Perfect Alphabetical Order

python - 从编辑器与控制台设置 pandas scatter_matrix 的轴限制

python - 自定义 Pyramid _formalchemy 形式

python - 左数据框与不同列中的匹配键合并

python - Pyhook 事件。注入(inject)?

python - Pyhook:一起使用 KeyboardEvents 和 MouseEvents

python - 根据 QLabel 包含的文本自动调整 QLabel 的大小

python - 如何避免 TypeError : MouseSwitch() missing 8 required positional arguments: 'msg' , 'x' 、 'y' 、 'data' 、 'time' 、 'hwnd' 和 'window_name'

python - pyHook KeyLogger 线程未完成