使用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 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控制字符

二进制

十进制

十六进制

缩写

可以显示的表示法

名称/意义

00000000

0

00

NUL


空字符(Null)

00000001

1

01

SOH


标题开始

00000010

2

02

STX


本文开始

00000011

3

03

ETX


本文结束

00000100

4

04

EOT


传输结束

00000101

5

05

ENQ


请求

00000110

6

06

ACK


确认回应

00000111

7

07

BEL


响铃

00001000

8

08

BS


退格

00001001

9

09

HT


水平定位符号

00001010

10

0A

LF


换行键

00001011

11

0B

VT


垂直定位符号

00001100

12

0C

FF


换页键

00001101

13

0D

CR


归位键

00001110

14

0E

SO


取消变换(Shift out)

00001111

15

0F

SI


启用变换(Shift in)

00010000

16

10

DLE


跳出数据通讯

00010001

17

11

DC1


设备控制一(XON 启用软件速度控制)

00010010

18

12

DC2


设备控制二

00010011

19

13

DC3


设备控制三(XOFF 停用软件速度控制)

00010100

20

14

DC4


设备控制四

00010101

21

15

NAK


确认失败回应

00010110

22

16

SYN


同步用暂停

00010111

23

17

ETB


区块传输结束

00011000

24

18

CAN


取消

00011001

25

19

EM


连接介质中断

00011010

26

1A

SUB


替换

00011011

27

1B

ESC


跳出

00011100

28

1C

FS


文件分割符

00011101

29

1D

GS


组群分隔符

00011110

30

1E

RS


记录分隔符

00011111

31

1F

US


单元分隔符

01111111

127

7F

DEL


删除

ASCII可显示字符



二进制

十进制

十六进制

图形

00100000

32

20

(空格)(␠)

00100001

33

21

!

00100010

34

22

"

00100011

35

23

#

00100100

36

24

$

00100101

37

25

%

00100110

38

26

&

00100111

39

27

'

00101000

40

28

(

00101001

41

29

)

00101010

42

2A

*

00101011

43

2B

+

00101100

44

2C

,

00101101

45

2D

-

00101110

46

2E

.

00101111

47

2F

/

00110000

48

30

0

00110001

49

31

1

00110010

50

32

2

00110011

51

33

3

00110100

52

34

4

00110101

53

35

5

00110110

54

36

6

00110111

55

37

7

00111000

56

38

8

00111001

57

39

9

00111010

58

3A

:

00111011

59

3B

;

00111100

60

3C

<

00111101

61

3D

=

00111110

62

3E

>

00111111

63

3F

?