python hook監聽事件python
做者:vpoet函數
mail:vpoet_sir@163.comspa
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()
運行截圖:.net
版權聲明:本文爲博主原創文章,未經博主容許不得轉載。code