python中使用pyhook实现键盘监控的例子


Posted in Python onJuly 18, 2014

pyhook下载:http://sourceforge.net/projects/pyhook/files/pyhook/1.5.1/

pyhookAPI手册: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__":

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

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

  #循环获取消息 
  pythoncom.PumpMessages(10000)
Python 相关文章推荐
Python实现的简单模板引擎功能示例
Sep 02 Python
Python实现螺旋矩阵的填充算法示例
Dec 28 Python
Python多重继承的方法解析执行顺序实例分析
May 26 Python
基于windows下pip安装python模块时报错总结
Jun 12 Python
Django框架使用内置方法实现登录功能详解
Jun 12 Python
10分钟用python搭建一个超好用的CMDB系统
Jul 17 Python
使用Python调取任意数字资产钱包余额功能
Aug 15 Python
python查看数据类型的方法
Oct 12 Python
python matplotlib:plt.scatter() 大小和颜色参数详解
Apr 14 Python
MATLAB数学建模之画图汇总
Jul 16 Python
Python使用shutil模块实现文件拷贝
Jul 31 Python
python 实现两个变量值进行交换的n种操作
Jun 02 Python
python使用pyhook监控键盘并实现切换歌曲的功能
Jul 18 #Python
python中使用百度音乐搜索的api下载指定歌曲的lrc歌词
Jul 18 #Python
python采集博客中上传的QQ截图文件
Jul 18 #Python
Python下singleton模式的实现方法
Jul 16 #Python
python的迭代器与生成器实例详解
Jul 16 #Python
Python的内存泄漏及gc模块的使用分析
Jul 16 #Python
Python的垃圾回收机制深入分析
Jul 16 #Python
You might like
索尼SONY SRF-S83/84电路分析和打磨
2021/03/02 无线电
php中看实例学正则表达式
2006/12/25 PHP
php 日期时间处理函数小结
2009/12/18 PHP
用PHP实现 上一篇、下一篇的代码
2012/09/29 PHP
php内存缓存实现方法
2015/01/24 PHP
谈谈PHP中substr和substring的正确用法及相关参数的介绍
2015/12/16 PHP
java解析json方法总结
2019/05/16 PHP
Dojo 学习笔记入门篇 First Dojo Example
2009/11/15 Javascript
3Z版基于jquery的图片复选框(asp.net+jquery)
2010/04/12 Javascript
Node.js中require的工作原理浅析
2014/06/24 Javascript
jQuery Masonry瀑布流插件使用详解
2014/11/17 Javascript
javascript中setInterval的用法
2015/07/19 Javascript
详解JavaScript的Polymer框架中的通知交互
2015/07/29 Javascript
JS实现的新浪微博大厅文字内容滚动效果代码
2015/11/05 Javascript
jquery ajaxfileupload异步上传插件使用详解
2017/02/08 Javascript
jQuery实现手势解锁密码特效
2017/08/14 jQuery
Vue 让元素抖动/摆动起来的实现代码
2018/05/31 Javascript
vue数组对象排序的实现代码
2018/06/20 Javascript
详解vue2.0模拟后台json数据
2019/05/16 Javascript
vue实现直播间点赞飘心效果的示例代码
2019/09/20 Javascript
js实现课堂随机点名系统
2019/11/21 Javascript
小程序简单两栏瀑布流效果的实现
2019/12/18 Javascript
原生js实现自定义消息提示框
2020/11/19 Javascript
Python中的map、reduce和filter浅析
2014/04/26 Python
PyQt5打开文件对话框QFileDialog实例代码
2018/02/07 Python
centos6.8安装python3.7无法import _ssl的解决方法
2018/09/17 Python
python 多线程将大文件分开下载后在合并的实例
2018/11/09 Python
详解python的argpare和click模块小结
2019/03/31 Python
python实现beta分布概率密度函数的方法
2019/07/08 Python
python之拟合的实现
2019/07/19 Python
余弦相似性计算及python代码实现过程解析
2019/09/18 Python
网络工程专业毕业生推荐信
2013/10/28 职场文书
外语学院毕业生的自我鉴定
2013/11/28 职场文书
承诺书样本
2014/08/30 职场文书
班主任班级管理心得体会
2016/01/07 职场文书
你知道哪几种MYSQL的连接查询
2021/06/03 MySQL