python hook监听事件

python hook监听事件

作者:vpoet

mail:vpoet_sir@163.com

 

 1 # -*- coding: utf-8 -*- # 
 2 # by oldj http://oldj.net/ #
 3 import pythoncom 
 4 import pyHook    
 5 def onMouseEvent(event): 
 6    # 监听鼠标事件     
 7    print "MessageName:",event.MessageName     
 8    print "Message:", event.Message     
 9    print "Time:", event.Time     
10    print "Window:", event.Window     
11    print "WindowName:", event.WindowName     
12    print "Position:", event.Position     
13    print "Wheel:", event.Wheel     
14    print "Injected:", event.Injected           
15    print"---"
16   
17    # 返回 True 以便将事件传给其它处理程序     
18    # 注意,这儿如果返回 False ,则鼠标事件将被全部拦截     
19    # 也就是说你的鼠标看起来会僵在那儿,似乎失去响应了     
20    return True
21  
22 def onKeyboardEvent(event):
23   # 监听键盘事件     
24    print "MessageName:", event.MessageName     
25    print "Message:", event.Message     
26    print "Time:", event.Time     
27    print "Window:", event.Window     
28    print "WindowName:", event.WindowName     
29    print "Ascii:", event.Ascii, chr(event.Ascii)     
30    print "Key:", event.Key     
31    print "KeyID:", event.KeyID     
32    print "ScanCode:", event.ScanCode     
33    print "Extended:", event.Extended     
34    print "Injected:", event.Injected     
35    print "Alt", event.Alt     
36    print "Transition", event.Transition     
37    print "---"      
38    # 同鼠标事件监听函数的返回值     
39    return True 
40 
41 def main():     
42    # 创建一个“钩子”管理对象     
43    hm = pyHook.HookManager()      
44    # 监听所有键盘事件     
45    hm.KeyDown = onKeyboardEvent     
46    # 设置键盘“钩子”     
47    hm.HookKeyboard()      
48    # 监听所有鼠标事件     
49    hm.MouseAll = onMouseEvent     
50    # 设置鼠标“钩子”     
51    hm.HookMouse()      
52    # 进入循环,如不手动关闭,程序将一直处于监听状态     
53    pythoncom.PumpMessages() 
54 
55 if __name__ == "__main__":     
56    main()

 

 


运行截图:



版权声明:本文为博主原创文章,未经博主允许不得转载。

 

posted @ 2015-06-24 12:47  vpoet  阅读(842)  评论(0编辑  收藏  举报