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中的Django框架中prefetch_related()函数对数据库查询的优化
Apr 01 Python
python实现bucket排序算法实例分析
May 04 Python
Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
Jan 11 Python
Python时间和字符串转换操作实例分析
Mar 16 Python
python批量下载抖音视频
Jun 17 Python
Django REST framework 分页的实现代码
Jun 19 Python
详解python列表(list)的使用技巧及高级操作
Aug 15 Python
django 快速启动数据库客户端程序的方法示例
Aug 16 Python
django 文件上传功能的相关实例代码(简单易懂)
Jan 22 Python
python3 中使用urllib问题以及urllib详解
Aug 03 Python
Python matplotlib模块及柱状图用法解析
Aug 10 Python
python+opencv实现目标跟踪过程
Jun 21 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
PHP学习资料汇总与网址
2007/03/16 PHP
PHP学习笔记之字符串编码的转换和判断
2014/05/22 PHP
ThinkPHP之foreach标签使用概述
2014/06/30 PHP
php多次include后导致全局变量global失效的解决方法
2015/02/28 PHP
php微信公众号开发(4)php实现自定义关键字回复
2016/12/15 PHP
PHP + plupload.js实现多图上传并显示进度条加删除实例代码
2017/03/06 PHP
php实现的rc4加密解密类定义与用法示例
2018/08/16 PHP
PHP7原生MySQL数据库操作实现代码
2020/07/03 PHP
犀利的js 函数集合
2009/06/11 Javascript
拖动布局之保存布局页面cookies篇
2010/10/29 Javascript
js浮动图片的动态效果
2013/07/10 Javascript
JavaScript限定图片显示大小的方法
2015/03/11 Javascript
jquery中attr和prop的区别分析
2015/03/16 Javascript
详解javascript实现自定义事件
2016/01/19 Javascript
JavaScript 拖拽实例代码
2016/09/21 Javascript
实例解析js中try、catch、finally的执行规则
2017/02/24 Javascript
AngularJS实现页面定时刷新
2017/03/14 Javascript
JavaScript中splice与slice的区别
2017/05/09 Javascript
一个简易的js图片轮播效果
2017/07/22 Javascript
Angular2管道Pipe及自定义管道格式数据用法实例分析
2017/11/29 Javascript
基于vue和websocket的多人在线聊天室
2020/02/01 Javascript
js实现无缝轮播图插件封装
2020/07/31 Javascript
JavaScript实现与web通信的方法详解
2020/08/07 Javascript
python检测远程服务器tcp端口的方法
2015/03/14 Python
Python实现将文本生成二维码的方法示例
2017/07/18 Python
Waterford美国官网:爱尔兰水晶制品品牌
2017/04/26 全球购物
法国面料和小百货在线商店:Mondial Tissus
2019/03/23 全球购物
毕业生在校学习的自我评价分享
2013/10/08 职场文书
民族团结先进个人事迹材料
2014/06/02 职场文书
巴西世界杯32强口号
2014/06/05 职场文书
2016优秀毕业生个人事迹材料
2016/02/29 职场文书
2019入党申请书范文3篇
2019/08/21 职场文书
Python 把两层列表展开平铺成一层(5种实现方式)
2021/04/07 Python
python实现大文本文件分割成多个小文件
2021/04/20 Python
ant design charts 获取后端接口数据展示
2022/05/25 Javascript
mysqldump进行数据备份详解
2022/07/15 MySQL