-
Notifications
You must be signed in to change notification settings - Fork 4
/
record.py
91 lines (74 loc) · 2.57 KB
/
record.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import time
import os
import sys
from functools import partial
import pyHook
import pythoncom
import pyautogui
def left_down(event, file_path):
x, y = event.Position
print ",".join(['left_down', str(x), str(y)])
with open(file_path, 'a') as f:
f.write(",".join(['left_down', str(x), str(y), str(time.time())]))
f.write('\n')
return True
def left_up(event, file_path):
x, y = event.Position
print ",".join(['left_up', str(x), str(y)])
with open(file_path, 'a') as f:
f.write(",".join(['left_up', str(x), str(y), str(time.time())]))
f.write('\n')
return True
def right_down(event, file_path):
x, y = event.Position
print ",".join(['right_down', str(x), str(y)])
with open(file_path, 'a') as f:
f.write(",".join(['right_down', str(x), str(y), str(time.time())]))
f.write('\n')
return True
def right_up(event, file_path):
x, y = event.Position
print ",".join(['right_up', str(x), str(y)])
with open(file_path, 'a') as f:
f.write(",".join(['right_up', str(x), str(y), str(time.time())]))
f.write('\n')
return True
def OnKeyboardEvent(event, file_path, dir_path):
if event.Ascii == 27: # Ascii('ESC')=27
print 'End of recording'
with open(file_path, 'a') as f:
f.write('done')
sys.exit()
return True
if __name__ == '__main__':
os.chdir(os.path.dirname(os.path.realpath(__file__)))
directory = 'mouse_recorder'
try:
session_name = sys.argv[1]
except:
print 'you must enter a name for the session\nfor example: python record.py session_name'
sys.exit()
dir_path = os.path.join(os.getcwd(), directory, session_name)
if not os.path.exists(dir_path):
os.makedirs(dir_path)
file_name = 'history.txt'
file_path = os.path.join(dir_path, file_name)
print dir_path
ld = partial(left_down, file_path=file_path)
lu = partial(left_up, file_path=file_path)
rd = partial(right_down, file_path=file_path)
ru = partial(right_up, file_path=file_path)
hm = pyHook.HookManager()
hm.SubscribeMouseLeftDown(ld)
hm.SubscribeMouseLeftUp(lu)
hm.SubscribeMouseRightDown(rd)
hm.SubscribeMouseRightUp(ru)
hm.HookMouse()
oke = partial(OnKeyboardEvent, file_path=file_path, dir_path=dir_path)
hm.KeyDown = oke
hm.HookKeyboard()
#while True:
# pythoncom.PumpWaitingMessages()
pythoncom.PumpMessages()
hm.UnhookMouse()
hm.UnHookKeyboard()