更多的问题在这里。基于pyHook's tutorial,函数中的.HookManager().OnMouseEvent事件变量具有.Injected属性。我找不到任何关于它的信息,有人知道它是什么吗?我试着去做
event.Injected = '<char to inject>'但它并没有起作用。
发布于 2019-05-24 01:28:25
免责声明:我不是这方面的专家,我只是分享我对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的库,而我没有这个库。
https://stackoverflow.com/questions/56128967
复制相似问题