This repository has been archived by the owner on Nov 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
direct_input.py
290 lines (251 loc) · 7.77 KB
/
direct_input.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
from random import choice
from time import sleep
import keyboard
import win32api
import win32con
import win32gui
from utils import (
Client,
ClientConfig,
MyFileHandler,
MyFormatter,
MyStreamHandler,
Path,
Settings,
Sg,
box,
ceil,
chunks,
compare,
copy,
datetime,
excepthook,
floor,
format_time,
get_font_name,
get_menu_pixels,
get_rotation,
get_text,
global_settings,
hdlr,
importlib,
json,
load_font,
log,
logger,
logging,
my_emit,
os,
requests,
rfh,
set_text,
sys,
timedelta,
)
MAP_VSC_TO_VK = 1
MAP_VK_TO_CHAR = 2
# noinspection PyPep8Naming
class VirtualInput:
def __init__(self, brawlhalla, hotkeys):
self.brawlhalla = brawlhalla
self.hotkeys = hotkeys
@property
def keys(self):
return vars(self.hotkeys)
def PressKey(self, hexCode):
win32gui.SendMessage(self.brawlhalla.window, win32con.WM_KEYDOWN, hexCode)
def ReleaseKey(self, hexCode):
win32gui.SendMessage(self.brawlhalla.window, win32con.WM_KEYUP, hexCode)
def press_key(self, *key_codes, delay=0.05):
for key_code in key_codes:
self.PressKey(key_code)
sleep(delay)
for key_code in key_codes:
self.ReleaseKey(key_code)
def release_keys(self):
for key in self.keys:
self.ReleaseKey(self.keys[key])
sleep(0.05)
def up(self, *args, **kwargs):
self.press_key(self.hotkeys.up, *args, **kwargs)
def down(self, *args, **kwargs):
self.press_key(self.hotkeys.down, *args, **kwargs)
def left(self, *args, **kwargs):
self.press_key(self.hotkeys.left, *args, **kwargs)
def right(self, *args, **kwargs):
self.press_key(self.hotkeys.right, *args, **kwargs)
def throw(self, *args, **kwargs):
self.press_key(self.hotkeys.throw, *args, **kwargs)
def quick(self, *args, **kwargs):
self.press_key(self.hotkeys.quick, *args, **kwargs)
def heavy(self, *args, **kwargs):
self.press_key(self.hotkeys.heavy, *args, **kwargs)
def dodge(self, *args, **kwargs):
self.press_key(self.hotkeys.dodge, *args, **kwargs)
def rbr(self, *args, **kwargs):
self.press_key(self.hotkeys.rbr, *args, **kwargs)
def esc(self, *args, **kwargs):
self.press_key(self.hotkeys.esc, *args, **kwargs)
def enter(self, *args, **kwargs):
self.press_key(self.hotkeys.enter, *args, **kwargs)
def fight(self):
move_keys = [
self.hotkeys.left,
self.hotkeys.up,
self.hotkeys.down,
self.hotkeys.right,
]
fight_keys = [
self.hotkeys.throw,
self.hotkeys.quick,
self.hotkeys.heavy,
self.hotkeys.dodge,
]
self.press_key(choice(move_keys), choice(fight_keys), delay=0.2)
class Hotkeys:
def __init__(self, hotkeys):
self.up = hotkeys.get("up", win32con.VK_UP)
self.down = hotkeys.get("down", win32con.VK_DOWN)
self.left = hotkeys.get("left", win32con.VK_LEFT)
self.right = hotkeys.get("right", win32con.VK_RIGHT)
self.throw = hotkeys.get("throw", 0x48)
self.quick = hotkeys.get("quick", 0x4A)
self.heavy = hotkeys.get("heavy", 0x4B)
self.dodge = hotkeys.get("dodge", 0x4C)
self.rbr = 0xDD
self.esc = win32con.VK_ESCAPE
self.enter = win32con.VK_RETURN
@classmethod
def load(cls):
try:
res = json.load(global_settings.hotkeys_location.open("r"))
return cls(res)
except FileNotFoundError:
return cls({})
def save(self):
try:
global_settings.hotkeys_location.parent.mkdir(parents=True, exist_ok=True)
json.dump(vars(self), global_settings.hotkeys_location.open("w+"))
except Exception as e:
logger.error("cant_save_hotkeys", e)
class GUIHotkeys:
def __init__(self):
self.hotkeys = Hotkeys.load()
self.window = self.create_window()
self.hook = None
self.last_keyboard_event = None
self.converter = {}
def hook_keyboard(self):
def inner_hook(event):
self.last_keyboard_event = event
if not self.hook:
self.hook = keyboard.hook(inner_hook)
def unhook_keyboard(self):
if self.hook:
keyboard.unhook(self.hook)
self.hook = None
@staticmethod
def text_column(*keys):
return Sg.Column(
[
[
Sg.Text(
" ",
size=(1, 1),
key=f"{key}_text",
font=(global_settings.font, 12),
)
]
for key in keys
]
)
def input_column(self, *keys):
return Sg.Column(
[
[
Sg.Input(
size=(3, 1),
key=key,
enable_events=True,
font=(global_settings.font, "12"),
default_text=self.vk_to_char(vars(self.hotkeys)[key]),
)
]
for key in keys
]
)
def combined_column(self, *keys):
return [self.text_column(*keys), self.input_column(*keys)]
def create_window(self):
layout = [
[
Sg.Text(
" ",
size=(1, 1),
key="hotkeys_title",
font=(global_settings.font, 20),
)
],
[
*self.combined_column("up", "left", "throw", "quick"),
*self.combined_column("down", "right", "dodge", "heavy"),
],
[
Sg.Button("", key="save", font=(global_settings.font, 12)),
Sg.Button("", key="back", font=(global_settings.font, 12)),
],
]
window = Sg.Window(
"",
layout,
size=(600, 350),
keep_on_top=True,
icon=global_settings.icon,
metadata="hotkeys_window_title",
).Finalize()
global_settings.update_window(window)
return window
@staticmethod
def vsc_to_vk(code):
return win32api.MapVirtualKey(code, MAP_VSC_TO_VK)
@staticmethod
def vk_to_char(code):
direct = {
win32con.VK_LEFT: "◁",
win32con.VK_UP: "△",
win32con.VK_RIGHT: "▷",
win32con.VK_DOWN: "▽",
}
if code in direct:
return direct[code]
return chr(win32api.MapVirtualKey(code, MAP_VK_TO_CHAR))
def save(self):
for key in self.converter:
vars(self.hotkeys)[key] = self.converter[key]
self.hotkeys.save()
def start_loop(self):
self.hook_keyboard()
while True:
event, values = self.window.read(timeout=50)
if self.last_keyboard_event:
if event in [
"up",
"down",
"left",
"right",
"quick",
"heavy",
"dodge",
"throw",
]:
vk = self.vsc_to_vk(self.last_keyboard_event.scan_code)
self.window[event].Update(self.vk_to_char(vk))
self.converter[event] = vk
if event in (Sg.WINDOW_CLOSED, "back"):
break
elif event == "save":
self.save()
break
global_settings.update_window(self.window)
self.unhook_keyboard()
self.window.close()