forked from taojy123/KeymouseGo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKeymouseGo.py
108 lines (78 loc) · 2.67 KB
/
KeymouseGo.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
#!/usr/bin/env python
#Boa:App:BoaApp
import Frame1
import wx
import time
import sys
import json
from pynput import mouse
from pynput import keyboard
from pynput.mouse import Button
from pynput.keyboard import Key, KeyCode
modules = {'Frame1': [1, 'Main frame of Application', u'Frame1.py']}
class BoaApp(wx.App):
def OnInit(self):
self.main = Frame1.create(None)
self.main.Show()
self.SetTopWindow(self.main)
return True
def main():
application = BoaApp(0)
application.MainLoop()
def single_run(script_path, run_times=1):
# python KeymouseGo.py scripts/0416_2342.txt 10
# KeymouseGo.exe scripts\0416_2342.txt
s = open(script_path, 'r').read()
s = json.loads(s)
steps = len(s)
mouse_ctl = mouse.Controller()
keyboard_ctl = keyboard.Controller()
j = 0
while j < run_times or run_times == 0:
j += 1
# Keep the same with Frame1.py:455, and remove code include `self`
for i in range(steps):
print(s[i])
# for old style script
if isinstance(s[i][0], str) and isinstance(s[i][3], int):
s[i].insert(0, s[i][3])
delay = s[i][0]
event_type = s[i][1]
message = s[i][2]
action = s[i][3]
message = message.lower()
time.sleep(delay / 1000.0)
if event_type == 'EM':
x, y = action
mouse_ctl.position = (x, y)
if message == 'mouse left down':
mouse_ctl.press(Button.left)
elif message == 'mouse left up':
mouse_ctl.release(Button.left)
elif message == 'mouse right down':
mouse_ctl.press(Button.right)
elif message == 'mouse right up':
mouse_ctl.release(Button.right)
else:
print('unknow mouse event:', message)
elif event_type == 'EK':
key_name = action
if len(key_name) == 1:
key = key_name
else:
key = getattr(Key, key_name)
if message == 'key down':
keyboard_ctl.press(key)
elif message == 'key up':
keyboard_ctl.release(key)
else:
print('unknow keyboard event:', message)
print('script run finish!')
if __name__ == '__main__':
print(sys.argv)
if len(sys.argv) > 1:
script_path = sys.argv[1]
run_times = int(sys.argv[2]) if len(sys.argv) > 2 else 1
single_run(script_path, run_times)
else:
main()