forked from intmian/quick_skill
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquick_mgr.py
More file actions
237 lines (222 loc) · 8.73 KB
/
Copy pathquick_mgr.py
File metadata and controls
237 lines (222 loc) · 8.73 KB
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import json
import keyboard
import random
import time
import os
from pynput import mouse
from ctypes import windll
mouseController = mouse.Controller()
user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
# TODO: 后面加一个自动识别游戏窗口的功能,避免在外面误触
class QuickCastManager:
def __init__(self):
self.settings = self.load_settings()
self.quick_casts = self.load_quick_casts()
self.save_settings()
self.save_quick_casts()
self.select_cast = None
self.mouse_combo = {}
self.lock = False
mouse_listener = mouse.Listener(on_click=self.on_click)
# 启动监听器
mouse_listener.start()
# 鼠标按键监听
def on_click(self, x,y, button, pressed):
"""
程序启动时就会启动鼠标监听,开始流程后,遍历所有方案,将鼠标相关注册到此处。
请注意除了输入处都是以汉字存储对应功能的。
"""
if not pressed:
return
print(self.mouse_combo)
if button == mouse.Button.x1:
if 'x1' not in self.mouse_combo:
return
self.run_combo(self.mouse_combo['x1'])
elif button == mouse.Button.x2:
if 'x2' not in self.mouse_combo:
return
self.run_combo(self.mouse_combo['x2'])
elif button == mouse.Button.left:
if 'MLeft' not in self.mouse_combo:
return
self.run_combo(self.mouse_combo['MLeft'])
elif button == mouse.Button.right:
if 'MRight' not in self.mouse_combo:
return
self.run_combo(self.mouse_combo['MRight'])
def load_settings(self):
try:
with open("setting.json", "r") as file:
return json.load(file)
except FileNotFoundError:
return {"key_up_interval": 0.01, "key_interval": 0.08}
def load_quick_casts(self):
try:
with open("quick.json", "r") as file:
return json.load(file)
except FileNotFoundError:
return {}
def save_quick_casts(self):
with open("quick.json", "w+") as file:
json.dump(self.quick_casts, file, indent=2)
def save_settings(self):
with open("setting.json", "w+") as file:
json.dump(self.settings, file, indent=2)
def change_settings(self, key_interval, key_up_interval):
self.settings["key_interval"] = key_interval
self.settings["key_up_interval"] = key_up_interval
self.save_settings()
def create_new_cast(self, name, combos):
self.quick_casts[name] = combos
self.save_quick_casts()
def delete_cast(self, name):
if name not in self.quick_casts:
return False
del self.quick_casts[name]
self.save_quick_casts()
return True
def delete_combo_from_cast(self, cast_name,trigger_key):
if cast_name not in self.quick_casts:
return False
find = False
for combo in self.quick_casts[cast_name]:
if combo["trigger_key"] == trigger_key:
self.quick_casts[cast_name].remove(combo)
find = True
break
self.save_quick_casts()
return find
def add_combo_to_cast(self, cast_name,trigger_key,sequence, hotkey=False):
if cast_name in self.quick_casts:
# 如果存在就覆盖
find = False
for combo in self.quick_casts[cast_name]:
if combo["trigger_key"] == trigger_key:
combo["sequence"] = sequence
find = True
break
if not find:
self.quick_casts[cast_name].append({"trigger_key": trigger_key, "sequence": sequence})
else:
return
if hotkey:
if trigger_key in ["x1","x2","MLeft","MRight"]:
self.mouse_combo[trigger_key] = {"trigger_key": trigger_key, "sequence": sequence}
else:
keyboard.add_hotkey("alt+" + trigger_key, self.run_combo, args=({"trigger_key": trigger_key, "sequence": sequence},))
self.save_quick_casts()
def run_listener(self,cast_name):
# 选择方案
if cast_name not in self.quick_casts:
return False
self.select_cast = cast_name
for combo in self.quick_casts[cast_name]:
if combo["trigger_key"] in ["x1","x2","MLeft","MRight"]:
self.mouse_combo[combo["trigger_key"]] = combo
else:
keyboard.add_hotkey(combo["trigger_key"], self.run_combo, args=(combo,))
self.cast_name = cast_name
return True
def stop_listener(self):
self.mouse_combo = {}
try:
keyboard.unhook_all_hotkeys()
except Exception as e:
find = False
for combo in self.quick_casts[self.cast_name]:
if combo["trigger_key"] not in ["x1","x2","MLeft","MRight"]:
find = True
break
if find:
return False
return True
def run_combo(self, combo):
if self.lock:
return
self.lock = True
# 如果当前存在alt按键被按下,等待按键释放
while keyboard.is_pressed('alt'):
time.sleep(0.001)
# for key in combo['sequence']:
# keyboard.press(key)
# delay = self.settings["key_interval"] * random.uniform(0.66, 1.33)
# time.sleep(delay)
# keyboard.release(key)
# delay = self.settings["key_up_interval"] * random.uniform(0.66, 1.33)
# time.sleep(delay)
timeline_now = 0
keys = []
for key in combo['sequence']:
if key[0] == "`":
delay = float(0)
# 为了提高准确度并且操作更加自然,随机延迟设定如下
delay += random.uniform(0.95, 1.051) * 0.001 * int(key[1:])
timeline_now += delay
continue
if len(key) >= 3 and (key[:2] == "lp" or key[:2] == "rp" or key[:2] == "x1" or key[:2] == "x2"):
delay = float(0)
delay += random.uniform(0.99, 1.01) * 0.001 * int(key[2:])
if key[0] == "l":
keys.append(("MLeft",True,timeline_now))
keys.append(("MLeft",False,timeline_now+delay))
if key[0] == "r":
keys.append(("MRight",True,timeline_now))
keys.append(("MRight",False,timeline_now+delay))
if key[:2] == "x1":
keys.append(("x1",True,timeline_now))
keys.append(("x1",False,timeline_now+delay))
if key[:2] == "x2":
keys.append(("x2",True,timeline_now))
keys.append(("x2",False,timeline_now+delay))
timeline_now += delay
continue
real_key = key
if key == "上":
real_key = "up"
elif key == "下":
real_key = "down"
elif key == "左":
real_key = "left"
elif key == "右":
real_key = "right"
keys.append((real_key,True,timeline_now))
delay = self.settings["key_up_interval"] * random.uniform(0.952, 1.05)
keys.append((real_key,False,timeline_now+delay))
timeline_now += self.settings["key_interval"] * random.uniform(0.82, 1.19)
keys.sort(key=lambda x: x[2])
for i,key in enumerate(keys):
print(key)
if i != 0:
time.sleep(key[2]-keys[i-1][2])
if key[0] == "MLeft":
if key[1]:
mouseController.press(mouse.Button.left)
else:
mouseController.release(mouse.Button.left)
continue
elif key[0] == "MRight":
if key[1]:
mouseController.press(mouse.Button.right)
else:
mouseController.release(mouse.Button.right)
continue
elif key[0] == "x1":
if key[1]:
mouseController.press(mouse.Button.x1)
else:
mouseController.release(mouse.Button.x1)
continue
elif key[0] == "x2":
if key[1]:
mouseController.press(mouse.Button.x2)
else:
mouseController.release(mouse.Button.x2)
continue
if key[1]:
keyboard.press(key[0])
else:
keyboard.release(key[0])
self.lock = False