-
Notifications
You must be signed in to change notification settings - Fork 6
/
fst_data_types.py
420 lines (332 loc) · 12.2 KB
/
fst_data_types.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
'''
Free-Snap-Tap V1.1.3
last updated: 241015-1041
'''
from abc import ABC, abstractmethod
# decorator data type_check
def type_check(expected_type):
def decorator(func):
def wrapper(self, value):
if not isinstance(value, expected_type):
raise TypeError(f"Expected {expected_type}, got {type(value)}")
return func(self, value)
return wrapper
return decorator
class Input_Event(ABC):
'''
#XXX
'''
def __init__(self, vk_code, constraints=[0,0], key_string = ''):
self._key_string = key_string
self._vk_code = vk_code
self._constraints = constraints
@property
def vk_code(self):
return self._vk_code
@property
def constraints(self):
return self._constraints
@property
def key_string(self):
return self._key_string
@abstractmethod
def __hash__(self):
pass
# to be able to use complimentory to the Key class and return 2 Key_events
@abstractmethod
def get_key_events(self):
pass
@abstractmethod
def _get_sign(self):
pass
@abstractmethod
def __eq__(self, other) -> bool:
pass
# def __str__(self):
# if self._key_string is None:
# return f"Key_Event({self._vk_code}, {self._is_press}, {self._constraints})"
# else:
# return f"Key_Event({self._key_string}, {self._is_press}, {self._constraints})"
def __repr__(self):
constraints = ''
value = 0
for constraint in self._constraints:
if isinstance(constraint, str):
constraints += f"|({constraint})"
if isinstance(constraint, int):
if value != constraint:
value = constraint
constraints += f"{'|' + str(constraint) if constraint != 0 else ''}"
if self._key_string is None:
return f"{self._get_sign()}{self._vk_code}{constraints}"
else:
return f"{self._get_sign()}{self._key_string}{constraints}"
class Key_Event(Input_Event):
'''
#XXX
'''
def __init__(self, vk_code, is_press=True, constraints=[0,0], key_string = None, is_toggle = False):
super().__init__(vk_code, constraints=constraints, key_string=key_string)
self._is_press = is_press
self._is_toggle = is_toggle
@property
def is_press(self):
return self._is_press
@property
def is_toggle(self):
return self._is_toggle
@is_toggle.setter
def is_toggle(self, new_bool):
self._is_toggle = new_bool
def get_all(self):
return self._vk_code, self._is_press, self._constraints
def __hash__(self):
# return hash((self._vk_code, self._state_pressed))
# return hash(f"{self._get_sign()}{self._vk_code}")
return hash(self.__repr__())
# to be able to use complimentory to the Key class and return 2 Key_events
def get_key_events(self):
return [self, self]
def get_opposite_key_event(self):
return Key_Event(self._vk_code, not self._is_press, self._constraints, self._key_string)
def __eq__(self, other) -> bool:
return (self.vk_code == other.vk_code) and (self.is_press is other.is_press)
def _get_sign(self):
return '-' if self._is_press else '+'
def repr_wo_constraints(self):
if self._key_string is None:
return f"{self._get_sign()}{self._vk_code}"
else:
return f"{self._get_sign()}{self._key_string}"
class Key(Input_Event):
'''
#XXX
'''
def __init__(self, vk_code, key_string='', constraints=[0,0], is_toggle=False):
super().__init__(vk_code, constraints=constraints, key_string=key_string)
self._is_toggle = is_toggle
self.key_event = Key_Event(self._vk_code, is_press=True, constraints=constraints, key_string=key_string)
if self._is_toggle:
self.key_event.is_toggle = True
self._key_events = [self.key_event, self.key_event]
else:
self._key_events = [self.key_event, self.key_event.get_opposite_key_event()]
@property
def is_toggle(self):
return self._is_toggle
def get_key_events(self):
return self._key_events
def _get_sign(self):
return '^' if self._is_toggle else ''
def __hash__(self):
return hash(f"{self._get_sign()}{self._vk_code}")
def __eq__(self, other) -> bool:
raise NotImplementedError
class Key_Group(object):
'''
#XXX
'''
def __init__(self, key_events=[]):
if isinstance(key_events, Key_Event):
key_events = [key_events]
self._key_events = key_events
@property
def key_events(self):
return self._key_events.copy()
@key_events.setter
@type_check(list)
def key_events(self, new_list):
self._key_events = new_list
def get_key_events(self):
return self._key_events
def get_vk_codes(self):
return [key.vk_codes for key in self._key_events]
def add_key_event(self, key_event):
self._key_events.append(key_event)
def append(self, key_event):
self._key_events.append(key_event)
def get_trigger(self):
return self._key_events[0]
def __hash__(self):
return hash(self.__repr__())
def __eq__(self, other) -> bool:
if len(self._key_events) == len(other.get_key_events()):
equal = True
for my_key_event, other_key_event in zip(self._key_events, other.get_key_events()):
equal = equal and my_key_event == other_key_event
return equal
else:
False
def __repr__(self):
key_strings = []
for key_event in self._key_events:
key_strings.append(repr(key_event))
return "KG(" + ', '.join(key_strings) + ")"
def __len__(self):
return len(self._key_events)
class Rebind(object):
def __init__(self, trigger_group, replacement):
self._trigger_group = trigger_group
self._replacement = replacement
self._alias = ''
@property
def alias(self):
return self._alias
@alias.setter
@type_check(str)
def alias(self, alias):
self._alias = alias
@property
def trigger_group(self):
return self._trigger_group
@trigger_group.setter
@type_check(Key_Group)
def trigger_group(self, new_list):
self._trigger_group = new_list
@property
def replacement(self):
return self._replacement
@replacement.setter
@type_check(Key_Event)
def replacement(self, new_list):
self._replacement = new_list
def get_trigger(self):
return self._trigger_group.get_trigger()
# def get_trigger_group(self):
# return self._trigger_group
def __hash__(self):
return hash(self.__repr__())
def __eq__(self, other):
equal = True
equal = equal and self.trigger_group == other.trigger_group
equal = equal and self.replacement == other.replacement
return equal
def __repr__(self):
return f"{self._alias} {self._trigger_group} : {self._replacement}"
class Macro(object):
def __init__(self, trigger_group, key_groups):
self._trigger_group = trigger_group
self._key_groups = key_groups
self._sequence_counter = 0
self._num_sequences = len(self._key_groups)
self._alias = ''
@property
def alias(self):
return self._alias
@alias.setter
@type_check(str)
def alias(self, alias):
self._alias = alias
@property
def trigger_group(self):
return self._trigger_group
@property
def num_sequences(self):
return self._num_sequences
@property
def sequence_counter(self):
return self._sequence_counter
def _get_key_group(self):
if self._num_sequences == 0:
raise ValueError("No key groups in Macro")
elif self._num_sequences == 1:
return self._key_groups[0]
else:
if self._sequence_counter >= self._num_sequences:
self._sequence_counter = 0
group = self._key_groups[self._sequence_counter]
self._sequence_counter += 1
return group
def get_key_events_of_current_sequence(self):
return self._get_key_group().get_key_events()
def reset_sequence_counter(self):
self._sequence_counter = 0
def get_sequence_counter(self):
return self._sequence_counter
def get_trigger(self):
return self._trigger_group.get_trigger()
def __hash__(self):
return hash(self.__repr__())
def __eq__(self, other):
raise NotImplementedError
def __repr__(self):
output = f"{self._alias} {self._trigger_group} :: {self._key_groups[0]}"
if self._num_sequences > 1:
inset = output.find('::')
for key_group in self._key_groups[1:]:
output += f"\n{' ' * inset} : {key_group}"
return output
# class Macro_Sequence(Macro):
# def __init__(self):
# super().__init__(self)
# def __hash__(self):
# return hash(self.__repr__())
# def __eq__(self, other):
# raise NotImplementedError
# def __repr__(self):
# raise NotImplementedError
class Tap_Group(object):
'''
#XXX
'''
def __init__(self, keys = []) -> None:
self.keys = keys
self.states = {}
self.active_key = None
for code in self.keys:
self.states[code] = 0
self.last_key_pressed = None
self.last_key_send = None
self._alias = ''
@property
def alias(self):
return self._alias
@alias.setter
@type_check(str)
def alias(self, alias):
self._alias = alias
def update_tap_states(self, vk_code, is_press):
if is_press:
self.states[vk_code] = 1
self.last_key_pressed = vk_code
else:
self.states[vk_code] = 0
self.active_key = self.get_key_to_send()
def get_vk_codes(self):
return [key.vk_code for key in self.keys]
def get_states(self):
return self.states
def get_last_key_pressed(self):
return self.last_key_pressed
def get_key_to_send(self):
"""
Determine which key to send based on the current state of the tap group.
- If no keys are pressed, no key is sent.
- If one key is pressed, that key is sent.
- If more than one key is pressed, the last pressed key is sent.
"""
num_of_keys_pressed = sum(self.states.values())
key_to_send = None
if num_of_keys_pressed == 1:
for key, state in self.states.items():
if state == 1:
key_to_send = key
elif num_of_keys_pressed > 1:
key_to_send = self.last_key_pressed
return key_to_send
def get_active_key(self):
return self.active_key
def get_last_key_send(self):
return self.last_key_send
def set_last_key_send(self, last_key_send):
self.last_key_send = last_key_send
# def __str__(self):
# key_strings = []
# for key in self.keys:
# key_strings.append(repr(key))
# return "Tap_Group(" + ','.join(key_strings) + ")"
def __repr__(self):
key_strings = []
for key in self.keys:
key_strings.append(repr(key))
return "Tap_Group(" + ','.join(key_strings) + ")"