python键盘、鼠标控制
使用pyhook模块可以很快地完成键盘及鼠标事件捕获。
网站上提供了个使用的例子,改写了下,将信息记录到文件中,本来想使用python的logging模块,但测试时发现,因为鼠标事件频率太高,导致写时报I/O错误的异常,所以使用了自己写文件记录日志的方式。
代码:
Python代码
- #!/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()
- #循环获取消息
- #关闭日志文件
- fobj.close()
复制代码
测试:
Plain
代码
- --------------------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表中查找即可