我正在尝试利用Windows Hooks来拦截和阻止我的应用程序发送自己的gui事件时的击键。
我想出了以下清单:
import pythoncom
import pyHook
import threading
import time
def on_keyboard_event(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 False
class WindowsHooksWrapper:
def __init__(self):
self.started = False
self.thread = threading.Thread(target=self.thread_proc)
self.hook_manager = pyHook.HookManager()
def start(self):
if self.started:
self.stop()
# Register hook
self.hook_manager.KeyDown = on_keyboard_event
self.hook_manager.KeyUp = on_keyboard_event
self.hook_manager.HookKeyboard()
# Start the windows message pump
self.started = True
self.thread.start()
def stop(self):
if not self.started:
return
self.started = False
self.thread.join()
self.hook_manager.UnhookKeyboard()
def thread_proc(self):
print "Thread started"
while self.started:
pythoncom.PumpWaitingMessages()
print "Thread exiting..."
class WindowsHooksWrapper2:
def __init__(self):
self.started = False
self.thread = threading.Thread(target=self.thread_proc)
def start(self):
if self.started:
self.stop()
self.started = True
self.thread.start()
def stop(self):
if not self.started:
return
self.started = False
self.thread.join()
def thread_proc(self):
print "Thread started"
# Evidently, the hook must be registered on the same thread with the windows msg pump or
# it will not work and no indication of error is seen
# Also note that for exception safety, when the hook manager goes out of scope, it
# unregisters all outstanding hooks
hook_manager = pyHook.HookManager()
hook_manager.KeyDown = on_keyboard_event
hook_manager.KeyUp = on_keyboard_event
hook_manager.HookKeyboard()
while self.started:
pythoncom.PumpWaitingMessages()
print "Thread exiting..."
self.hook_manager.UnhookKeyboard()
def main():
# hook_manager = pyHook.HookManager()
# hook_manager.KeyDown = on_keyboard_event
# hook_manager.KeyUp = on_keyboard_event
# hook_manager.HookKeyboard()
# pythoncom.PumpMessages()
hook_wrapper = WindowsHooksWrapper2()
hook_wrapper.start()
time.sleep(30)
hook_wrapper.stop()
if __name__ == "__main__":
main()
main中注释掉的部分来自pyhook Wiki教程,它工作正常。
然后我尝试将其集成到一个类中,即“ WindowsHooksWrapper”类。如果我使用了该类,它将无法正常工作,并且键盘消息会到达其预期的目标。
直觉上,我然后尝试了“ WindowsHooksWrapper2”,在这里我将钩子的注册与消息泵一起移到了同一线程。现在可以使用了。
我的直觉是否正确,要求注册与泵在同一线程上?如果是这样,为什么?
请注意,我有一种感觉,这是Windows 32 API的要求,而不是python或pyhook库本身,因为我是用C ++编写的,并且直接使用'SetWindowsHook'获得了相同的结果。
答案 0 :(得分:1)
您已经创建了一个线程作用域挂钩。
在Python中这些挂钩事件与特定线程或与调用线程在同一桌面上的所有线程相关联。
pythoncom.PumpWaitingMessages()
和Win32中GetMessage
/ PeekMessage
是从“特定线程或与调用线程在同一桌面上的所有线程”获取消息的方法。 >
要创建一个全局钩子,为了使键盘钩子可从所有进程访问,必须将其放置在DLL中,然后将其加载到每个进程的地址空间中。有关如何制作全局键盘挂钩的详细信息,请参见此Answer。