forked from pywinauto/pywinauto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recorder.py
143 lines (110 loc) · 3.74 KB
/
recorder.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
from pywinauto.application import Application
import pythoncom, pyHook
mouse_mapping = {
'mouse move': None,
'mouse right down': True,
'mouse right up': ("right_click_input"),
'mouse left down': True,
'mouse left up': None,
'mouse wheel': None,
}
last_message = None
last_event_time = 0
def OnMouseEvent(event):
global last_event_time
# if this is the first action - remember the start time of the script
if not last_event_time:
last_event_time = event.Time
app = Application()
# wrap the window that is coming from the event
if event.Window:
wrapped = app.window(handle = event.Window)
else:
wrapped = None
# if there was no previous message
global last_message
if last_message is None:
last_message = event.MessageName
return True
# get the button pressed
button = ""
if "right" in event.MessageName and "right" in last_message:
button = "Right"
elif "left" in event.MessageName and "left" in last_message:
button = "Left"
toplevel = ""
if wrapped and not wrapped.IsDialog():
toplevel = '.Window_(title = "%s", class_name = "%s")' %(
wrapped.TopLevelParent().window_text(), wrapped.TopLevelParent().class_name())
if "up" in event.MessageName and "down" in last_message:
print "time.sleep(%d)"% (event.Time - last_event_time)
print 'app%s.Window_(title = "%s", class_name = "%s").%sClickInput()'%(
toplevel,
wrapped.WindowText(),
wrapped.Class(),
button)
last_event_time = event.Time
last_message = event.MessageName
# called when mouse events are received
#print 'MessageName:',event.MessageName
# print 'Message:',event.Message
# print 'Time:',event.Time
#print 'Window:',event.Window
# print 'WindowName:',event.WindowName
# print 'Position:',event.Position
# print 'Wheel:',event.Wheel
# print 'Injected:',event.Injected
# print '---'
# return True to pass the event to other handlers
return True
def OnKeyboardEvent(event):
global last_event_time
# if this is the first action - remember the start time of the script
if not last_event_time:
last_event_time = event.Time
#print dir(event)
app = Application()
if event.Window:
wrapped = app.window(handle = event.Window)
else:
pass
global last_message
if last_message is None:
last_message = event.MessageName
return True
if "down" in event.MessageName:
print "time.sleep(%d)"% (event.Time - last_event_time)
print 'app.Window_(title = "%s", class_name = "%s").Typekeys("%s")'%(
wrapped.window_text(),
wrapped.class_name(),
`event.Key`)
last_event_time = event.Time
print 'MessageName:',event.MessageName
# print 'Message:',event.Message
# print 'Time:',event.Time
# print 'Window:',event.Window
# print 'WindowName:',event.WindowName
win = event.WindowName
# print 'Ascii:', event.Ascii, chr(event.Ascii)
# print 'Key:', event.Key
# print 'KeyID:', event.KeyID
# print 'ScanCode:', event.ScanCode
# print 'Extended:', event.Extended
# print 'Injected:', event.Injected
# print 'Alt', event.Alt
# print 'Transition', event.Transition
# print '---'
# return True to pass the event to other handlers
return True
# create a hook manager
hm = pyHook.HookManager()
# watch for all mouse events
hm.MouseAll = OnMouseEvent
# set the hook
hm.HookMouse()
# watch for all mouse events
hm.KeyAll = OnKeyboardEvent
# set the hook
hm.HookKeyboard()
# wait forever
pythoncom.PumpMessages()