前言
我之前在做一个一个记录鼠标键盘信息的小玩意儿的时候
发现python有现成的包可以用,用起来十分方便
一些说明
先说要安装的包,需要安装pyHook和pywin32,pip install 暂时不支持
可以自行到https://www.lfd.uci.edu/~gohlke/pythonlibs/ 手动下载
记得要根据环境自己下载对应版本
我说一下,以下的代码都是在py2.7的环境下运行的,主要是因为pyhook3的包对python3其实并不支持,会出现【TypeError: MouseSwitch() missing 8 required positional arguments: ‘msg’, ‘x’, ‘y’, ‘data’, ‘time’, ‘hwnd’, and ‘window_name’】这一报错
关键函数
处理鼠标的事件1
2
3
4
5
6
7
8
9
10
11def (event):
fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + 'n')
fobj.writelines("Current Time:%sn" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
fobj.writelines("MessageName:%sn" % str(event.MessageName))
fobj.writelines("Message:%dn" % event.Message)
fobj.writelines("Time_sec:%dn" % event.Time)
fobj.writelines("Window:%sn" % str(event.Window))
fobj.writelines("WindowName:%sn" % str(event.WindowName))
fobj.writelines("Position:%sn" % str(event.Position))
fobj.writelines('MouseEvent End' + 'n')
return True
处理键盘事件1
2
3
4
5
6
7
8
9
10
11
12
13
14def onKeyboardEvent(event):
"处理键盘事件"
fobj.writelines('-' * 20 + 'Keyboard Begin' + '-' * 20 + 'n')
fobj.writelines("Current Time:%sn" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
fobj.writelines("MessageName:%sn" % str(event.MessageName))
fobj.writelines("Message:%dn" % event.Message)
fobj.writelines("Time:%dn" % event.Time)
fobj.writelines("Window:%sn" % str(event.Window))
fobj.writelines("WindowName:%sn" % str(event.WindowName))
fobj.writelines("Ascii_code: %dn" % event.Ascii)
fobj.writelines("Ascii_char:%sn" % chr(event.Ascii))
fobj.writelines("Key:%sn" % str(event.Key))
fobj.writelines( 'Keyboard End' + 'n')
return True
我是直接输出到txt的,觉得我的输出太累赘的话可以去掉一部分不必要的信息
整体源代码1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63import pythoncom
import pyHook
import time
import Tkinter as tk
def (event):
"处理鼠标事件"
fobj.writelines('-' * 20 + 'MouseEvent Begin' + '-' * 20 + 'n')
fobj.writelines("Current Time:%sn" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
fobj.writelines("MessageName:%sn" % str(event.MessageName))
fobj.writelines("Message:%dn" % event.Message)
fobj.writelines("Time_sec:%dn" % event.Time)
fobj.writelines("Window:%sn" % str(event.Window))
fobj.writelines("WindowName:%sn" % str(event.WindowName))
fobj.writelines("Position:%sn" % str(event.Position))
fobj.writelines( 'MouseEvent End' + 'n')
return True
def onKeyboardEvent(event):
"处理键盘事件"
fobj.writelines('-' * 20 + 'Keyboard Begin' + '-' * 20 + 'n')
fobj.writelines("Current Time:%sn" % time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime()))
fobj.writelines("MessageName:%sn" % str(event.MessageName))
fobj.writelines("Message:%dn" % event.Message)
fobj.writelines("Time:%dn" % event.Time)
fobj.writelines("Window:%sn" % str(event.Window))
fobj.writelines("WindowName:%sn" % str(event.WindowName))
fobj.writelines("Ascii_code: %dn" % event.Ascii)
fobj.writelines("Ascii_char:%sn" % chr(event.Ascii))
fobj.writelines("Key:%sn" % str(event.Key))
fobj.writelines( 'Keyboard End' +'n')
return True
def newFile():
file=open("log.txt","a")
file.close()
def mainhook():
#创建hook句柄
hm = pyHook.HookManager()
#监控键盘
hm.KeyDown = onKeyboardEvent
hm.HookKeyboard()
#监控鼠标
hm.MouseAll = onMouseEvent
hm.HookMouse()
#循环获取消息
pythoncom.PumpMessages()
#关闭日志文件
fobj.close()
def my_exit():
sys.exit()
file_name = "D:ProgramDatapython_codepy_hooklog.txt"
fobj = open(file_name, 'w')
window = tk.Tk()
window.title('My UIA demo ')
window.geometry('500x300')
button1 = tk.Button(window, text='newFile', font=('Arial', 12), width=10, height=1, command=newFile)
button1.pack()
button2 = tk.Button(window, text='Begin Hook', font=('Arial', 12), width=10, height=1, command=mainhook)
button2.pack()
button3 = tk.Button(window, text='Exit', font=('Arial', 12), width=10, height=1, command=my_exit)
button3.pack()
window.mainloop()
对代码的一些补充
本来我打算用qt好好做个界面的,后来因为有事鸽了,就随便用Tkinter放了几个按钮上去,非常丑,以后有空可能会改一改后面的代码(咕咕咕)
以上内容仅为个人的学习笔记,如果有内容错漏,欢迎大家联系笔者讨论