挂钩event.Injected?
Pyhook event.Injected?
这里确实是一个问题。基于 pyHook's tutorial,函数中的 .HookManager().OnMouseEvent 事件变量具有 .Injected 属性。没找到相关资料,请问有人知道是什么吗?我试过
event.Injected = '<char to inject>'
但是没用。
免责声明:我不是这方面的专家,
我只是分享我对 tutorial and the documentation 的观察,
希望对您有所帮助。
event
上的属性不让你手动设置,
但供您的事件处理程序阅读和采取行动。
如您在 KeyboardEvent and MouseEvent 的文档中所见,
Injected
实例变量的目的是检查事件是否以编程方式生成。
我认为,这意味着您的处理程序从鼠标和键盘 activity 接收到的事件将始终具有此变量 False
。
并且有一种以编程方式生成事件的方法,
我想是为了测试您的处理程序。
该方法似乎是 HookManager.KeyboardSwitch and 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 的库,我没有。
这里确实是一个问题。基于 pyHook's tutorial,函数中的 .HookManager().OnMouseEvent 事件变量具有 .Injected 属性。没找到相关资料,请问有人知道是什么吗?我试过
event.Injected = '<char to inject>'
但是没用。
免责声明:我不是这方面的专家, 我只是分享我对 tutorial and the documentation 的观察, 希望对您有所帮助。
event
上的属性不让你手动设置,
但供您的事件处理程序阅读和采取行动。
如您在 KeyboardEvent and MouseEvent 的文档中所见,
Injected
实例变量的目的是检查事件是否以编程方式生成。
我认为,这意味着您的处理程序从鼠标和键盘 activity 接收到的事件将始终具有此变量 False
。
并且有一种以编程方式生成事件的方法,
我想是为了测试您的处理程序。
该方法似乎是 HookManager.KeyboardSwitch and 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 的库,我没有。