Python實(shí)現(xiàn)監(jiān)控鍵盤鼠標(biāo)操作示例【基于pyHook與pythoncom模塊】
本文實(shí)例講述了Python實(shí)現(xiàn)監(jiān)控鍵盤鼠標(biāo)操作。分享給大家供大家參考,具體如下:
# -*- coding: utf-8 -*- import pythoncom import pyHook import time def onMouseEvent(event): "處理鼠標(biāo)事件" fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + '\n') fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())) fobj.writelines("MessageName:%s\n" % str(event.MessageName)) fobj.writelines("Message:%d\n" % event.Message) fobj.writelines("Time_sec:%d\n" % event.Time) fobj.writelines("Window:%s\n" % str(event.Window)) fobj.writelines("WindowName:%s\n" % str(event.WindowName)) fobj.writelines("Position:%s\n" % str(event.Position)) fobj.writelines('-' * 20 + 'MouseEvent End' + '-' * 20 + '\n') return True def onKeyboardEvent(event): "處理鍵盤事件" fobj.writelines('-' * 20 + 'Keyboard Begin' + '-' * 20 + '\n') fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())) fobj.writelines("MessageName:%s\n" % str(event.MessageName)) fobj.writelines("Message:%d\n" % event.Message) fobj.writelines("Time:%d\n" % event.Time) fobj.writelines("Window:%s\n" % str(event.Window)) fobj.writelines("WindowName:%s\n" % str(event.WindowName)) fobj.writelines("Ascii_code: %d\n" % event.Ascii) fobj.writelines("Ascii_char:%s\n" % chr(event.Ascii)) fobj.writelines("Key:%s\n" % str(event.Key)) fobj.writelines('-' * 20 + 'Keyboard End' + '-' * 20 + '\n') return True #打開(kāi)日志文件 file_name = "E:\\hook_log.txt" fobj = open(file_name, 'w') #創(chuàng)建hook句柄 hm = pyHook.HookManager() #監(jiān)控鍵盤 hm.KeyDown = onKeyboardEvent hm.HookKeyboard() #監(jiān)控鼠標(biāo) hm.MouseAll = onMouseEvent hm.HookMouse() #循環(huán)獲取消息 pythoncom.PumpMessages() #關(guān)閉日志文件 fobj.close()
運(yùn)行后可在E盤根目錄下創(chuàng)建一個(gè)hook_log.txt文件,用于記錄監(jiān)控鍵盤、鼠標(biāo)操作信息。如下圖所示:
注:代碼中所用到的pythoncom與pyHook模塊可點(diǎn)擊此處下載響應(yīng)版本的whl文件再進(jìn)入whl文件目錄使用pip install
命令進(jìn)行安裝
更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
設(shè)置jupyter中DataFrame的顯示限制方式
這篇文章主要介紹了設(shè)置jupyter中DataFrame的顯示限制方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)2020-04-04pytorch 實(shí)現(xiàn)L2和L1正則化regularization的操作
這篇文章主要介紹了pytorch 實(shí)現(xiàn)L2和L1正則化regularization的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-03-03各個(gè)系統(tǒng)下的Python解釋器相關(guān)安裝方法
這篇文章主要介紹了各個(gè)系統(tǒng)下的Python解釋器相關(guān)安裝方法,Python在很多Linux發(fā)行版中已經(jīng)被默認(rèn)安裝,需要的朋友可以參考下2015-10-10如何用Python提取10000份log中的產(chǎn)品信息
這篇文章主要介紹了如何用Python提取10000份log中的產(chǎn)品信息,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2021-01-01Python tkinter進(jìn)度條控件(Progressbar)的使用
這篇文章主要介紹了Python tkinter進(jìn)度條控件(Progressbar)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04