您好,登錄后才能下訂單哦!
本文實(shí)例為大家分享了python hook監(jiān)聽事件的具體代碼,供大家參考,具體內(nèi)容如下
# -*- coding: utf-8 -*- # # by oldj http://oldj.net/ # import pythoncom import pyHook def onMouseEvent(event): # 監(jiān)聽鼠標(biāo)事件 print "MessageName:",event.MessageName print "Message:", event.Message print "Time:", event.Time print "Window:", event.Window print "WindowName:", event.WindowName print "Position:", event.Position print "Wheel:", event.Wheel print "Injected:", event.Injected print"---" # 返回 True 以便將事件傳給其它處理程序 # 注意,這兒如果返回 False ,則鼠標(biāo)事件將被全部攔截 # 也就是說(shuō)你的鼠標(biāo)看起來(lái)會(huì)僵在那兒,似乎失去響應(yīng)了 return True def onKeyboardEvent(event): # 監(jiān)聽鍵盤事件 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 "---" # 同鼠標(biāo)事件監(jiān)聽函數(shù)的返回值 return True def main(): # 創(chuàng)建一個(gè)“鉤子”管理對(duì)象 hm = pyHook.HookManager() # 監(jiān)聽所有鍵盤事件 hm.KeyDown = onKeyboardEvent # 設(shè)置鍵盤“鉤子” hm.HookKeyboard() # 監(jiān)聽所有鼠標(biāo)事件 hm.MouseAll = onMouseEvent # 設(shè)置鼠標(biāo)“鉤子” hm.HookMouse() # 進(jìn)入循環(huán),如不手動(dòng)關(guān)閉,程序?qū)⒁恢碧幱诒O(jiān)聽狀態(tài) pythoncom.PumpMessages() if __name__ == "__main__": main()
運(yùn)行截圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。