首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Pyhook event.Injected?

Pyhook event.Injected?
EN

Stack Overflow用户
提问于 2019-05-14 19:04:43
回答 1查看 402关注 0票数 1

更多的问题在这里。基于pyHook's tutorial,函数中的.HookManager().OnMouseEvent事件变量具有.Injected属性。我找不到任何关于它的信息,有人知道它是什么吗?我试着去做

代码语言:javascript
复制
event.Injected = '<char to inject>'

但它并没有起作用。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-24 01:28:25

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

event上的属性不是供您手动设置的,而是供您的事件处理程序读取和操作的。

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

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

代码语言:javascript
复制
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组合键终止程序。

然后,要以编程方式生成一些事件并查看它们的外观,请尝试如下所示:

代码语言:javascript
复制
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 OS的库,而我没有这个库。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56128967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档