【发布时间】:2019-05-14 11:04:43
【问题描述】:
这里真的是一个更多的问题。基于pyHook's tutorial,函数中的 .HookManager().OnMouseEvent 事件变量具有 .Injected 属性。没找到相关资料,有大佬知道是什么吗?我试过做
event.Injected = '<char to inject>'
但是没有用。
【问题讨论】:
这里真的是一个更多的问题。基于pyHook's tutorial,函数中的 .HookManager().OnMouseEvent 事件变量具有 .Injected 属性。没找到相关资料,有大佬知道是什么吗?我试过做
event.Injected = '<char to inject>'
但是没有用。
【问题讨论】:
免责声明:我不是这方面的专家, 我只是分享我对tutorial 和documentation 的观察, 希望对您有所帮助。
event上的属性不是你手动设置的,
但要让您的事件处理程序读取并采取行动。
正如您在KeyboardEvent 和MouseEvent 的文档中看到的,
Injected 实例变量的目的是检查事件是否以编程方式生成。
我认为,这意味着您的处理程序从鼠标和键盘活动接收的事件将始终具有此变量False。
还有一种以编程方式生成事件的方法,
我想是为了测试你的处理程序。
而且方法好像是HookManager.KeyboardSwitch和HookManager.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 OS 的库,我没有。
【讨论】: