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

标签 python pyhook

这里确实有更多问题。基于pyHook's tutorial ,函数中的 .HookManager().OnMouseEvent 事件变量具有 .Injected 属性。我没找到相关信息,有谁知道这是什么吗?我尝试做

event.Injected = '<char to inject>'

但是没有成功。

最佳答案

免责声明:我不是这方面的专家, 我只是分享我对 tutorial 的观察和 documentation , 希望对您有所帮助。

事件的属性不适合您手动设置, 但供您的事件处理程序读取并执行操作。

正如您在 KeyboardEvent 的文档中看到的那样和 MouseEvent , Injected 实例变量的目的是检查事件是否以编程方式生成。 我认为,这意味着您的处理程序从鼠标和键盘事件接收到的事件将始终具有此变量False。 有一种方法可以以编程方式生成事件, 我想是为了测试你的处理程序。 该方法似乎是 HookManager.KeyboardSwitchHookManager.MouseSwitch .

例如尝试一下这个。创建一个简单的程序来查看一些真实键盘事件的详细信息:

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 key press events
hm.KeyDown = OnKeyboardEvent

# set the hook
hm.HookKeyboard()

# wait forever
pythoncom.PumpMessages()

按几个键并观察输出。按 Control-C 终止程序。

然后,以编程方式生成一些事件并查看它们的样子, 尝试这样的事情:

import pythoncom, pyHook

def OnKeyboardEvent(event):
    # ... same code as in previous sample ...

# create a hook manager
hm = pyHook.HookManager()

# watch for key press events
hm.KeyDown = OnKeyboardEvent

# set the hook
hm.HookKeyboard()

# send keyboard event programmatically
msg = ...       # a value like in the "Message: ..." output in the previous run
vk_code = ...   # a value like in the "KeyID: ..." output in the previous run
scan_code = ... # a value like in the "ScanCode: ..." output in the previous run
ascii = ...     # a value like in the "Ascii: ..." output in the previous run
flags = 0x10    # see http://pyhook.sourceforge.net/doc_1.5.0/pyhook.HookManager-pysrc.html#KeyboardEvent.IsInjected
time = ...      # a value like in the "Time: ..." output in the previous run
hwnd = ...      # a value like in the "Window: ..." output in the previous run
win_name = ...  # a value like in the "WindowName: ..." output in the previous run
hm.KeyboardSwitch(msg, vk_code, scan_code, ascii, flags, time, hwnd, win_name)

设置适当的值后, 并运行这个程序, 您应该在输出中看到“Injected: True”。

我认为这是基本思想,对于鼠标事件也是如此。 不幸的是我无法测试这个, 因为 pyHook 似乎是 Windows 操作系统的一个库,而我没有。

关于python - Pyhook 事件。注入(inject)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56128967/

相关文章:

python - pyqt 和 qt 设计器的最小示例 qtreeview

python - 使用一个数据框中的列标题和值来查找另一个数据框中的权重

python - cx_freeze 不适用于 pyHooks

javascript - 如何将以下代码(例如\u00e8,\u00e9)以字符串格式解码并输出到Python中的符号

python - 在 Python 中从文件夹中读取 HTML 文件

python - 使用 Python regex 模块 re 时必须转义哪些特殊字符?

python - win32api & pyhook - 如何获取用户的打字语言?

当使用 Hook 到某些应用程序时,pythoncom 在 KeyDown 上崩溃

python - 无法安装 Pyhook Python 3.8.1

python - 使用pyw文件的Windows启动不会关闭cmd