-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcombo_tracker.pyw
More file actions
917 lines (780 loc) · 34.5 KB
/
combo_tracker.pyw
File metadata and controls
917 lines (780 loc) · 34.5 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
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
import tkinter as tk
from tkinter import ttk, messagebox
from pynput import keyboard, mouse
import time
import threading
import json
import os
import sys
from pathlib import Path
# no need for backwards compatibility with previous versions of the code. we are still in development phase
class ComboTrackerApp:
def __init__(self, root):
self.root = root
self.root.title("Combo Trainer (Mouse & Keyboard)")
self.root.geometry("700x650")
# --- Data & State ---
self.combos = {}
self.active_combo_name = None
self.active_combo_tokens = []
self.active_combo_steps = []
self.current_index = 0
self.start_time = 0
self.last_input_time = 0
self.is_listening = True
self.hold_in_progress = False
self.hold_expected_input = None
self.hold_started_at = 0.0
self.hold_row_id = None
self.hold_required_ms = None
self.wait_in_progress = False
self.wait_started_at = 0.0
self.wait_until = 0.0
self.wait_row_id = None
self.wait_required_ms = None
self.currently_pressed = set()
self.attempt_counter = 0
# Combo enders: key -> grace_ms (0 means no grace; wrong press drops immediately)
self.combo_enders = {}
self.last_success_input = None
# --- Persistence ---
self.data_dir = self._get_data_dir()
self.save_path = self.data_dir / "combos.json"
# --- UI Layout ---
# 1. Add New Combo
frame_add = tk.LabelFrame(root, text="Add New Combo", padx=10, pady=10)
frame_add.pack(fill="x", padx=10, pady=5)
frame_add.grid_columnconfigure(1, weight=1)
frame_add.grid_columnconfigure(2, weight=1)
tk.Label(frame_add, text="Name:").grid(row=0, column=0, sticky="w")
self.entry_name = tk.Entry(frame_add, width=15)
self.entry_name.grid(row=0, column=1, columnspan=2, padx=5, sticky="w")
tk.Label(frame_add, text="Inputs (comma sep):").grid(row=1, column=0, sticky="w", pady=(10, 0))
self.entry_keys = tk.Entry(frame_add, width=45)
self.entry_keys.grid(row=2, column=0, columnspan=3, padx=5, sticky="ew")
# Helper text for mouse inputs
lbl_help = tk.Label(
frame_add,
text="LMB, RMB, MMB for mouse. Hold syntax: hold(space,0.2) or space{0.2}\n"
"Wait syntax: wait:0.5 (Decimals are seconds; append 'ms' for milliseconds).\n"
"Example: a, w, wait:0.2, LMB, hold(space,0.3), d, RMB",
fg="gray",
font=("Arial", 8),
)
lbl_help.grid(row=3, column=0, columnspan=3, sticky="w", padx=5, pady=(10, 0))
tk.Label(frame_add, text="Combo enders (key:seconds):").grid(row=4, column=0, sticky="w", pady=(10, 0))
self.entry_enders = tk.Entry(frame_add, width=45)
self.entry_enders.grid(row=4, column=1, columnspan=2, padx=5, sticky="ew")
btn_save = tk.Button(frame_add, text="Save / Update", command=self.save_combo)
btn_save.grid(row=0, column=4, padx=5)
btn_new = tk.Button(frame_add, text="New", command=self.new_combo)
btn_new.grid(row=0, column=5, padx=5)
# 2. Select Combo
frame_select = tk.LabelFrame(root, text="Practice", padx=10, pady=10)
frame_select.pack(fill="x", padx=10, pady=5)
tk.Label(frame_select, text="Select Combo:").pack(side="left")
self.combo_selector = ttk.Combobox(frame_select, state="readonly")
self.combo_selector.pack(side="left", padx=10, fill="x", expand=True)
self.combo_selector.bind("<<ComboboxSelected>>", self.set_active_combo)
btn_delete = tk.Button(frame_select, text="Delete", command=self.delete_active_combo)
btn_delete.pack(side="left", padx=5)
# 3. Status
self.lbl_status = tk.Label(root, text="Status: Select a combo to start", font=("Arial", 12, "bold"), fg="gray")
self.lbl_status.pack(pady=10)
self.lbl_min_time = tk.Label(root, text="Fastest possible: —", font=("Arial", 10), fg="gray40")
self.lbl_min_time.pack(pady=(0, 10))
# 4. Results Table
self.tree = ttk.Treeview(root, columns=("Input", "Split (ms)", "Total (ms)"), show="headings")
self.tree.heading("Input", text="Input Pressed")
self.tree.heading("Split (ms)", text="Gap (ms)")
self.tree.heading("Total (ms)", text="Time from Start (ms)")
self.tree.pack(fill="both", expand=True, padx=10, pady=10)
self.tree.tag_configure("sep", foreground="gray50")
# --- Listeners ---
# We start both listeners. They run in separate threads automatically.
self.key_listener = keyboard.Listener(on_press=self.on_key_press, on_release=self.on_key_release)
self.mouse_listener = mouse.Listener(on_click=self.on_mouse_click)
self.key_listener.start()
self.mouse_listener.start()
# Load saved combos (if any) and hook window close to save.
self.load_combos()
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
# Recompute min-time while editing.
self.entry_keys.bind("<KeyRelease>", lambda _e: self.update_min_time_from_editor())
# --- Input Normalization ---
def normalize_key(self, key):
"""Convert keyboard events to readable strings"""
try:
return key.char.lower()
except AttributeError:
return key.name.lower() # e.g., space, enter, shift
def normalize_mouse(self, button):
"""Convert mouse objects to LMB, RMB, MMB strings"""
if button == mouse.Button.left:
return "lmb"
elif button == mouse.Button.right:
return "rmb"
elif button == mouse.Button.middle:
return "mmb"
else:
return "mouse_extra"
# --- Core Logic ---
def split_inputs(self, keys_str: str):
"""
Split the Inputs field into tokens by commas, but do NOT split commas that are
inside hold(...) parentheses or inside {...} braces.
Example:
"f, e, hold(e, 0.2), r" -> ["f", "e", "hold(e, 0.2)", "r"]
"""
s = keys_str or ""
out = []
buf = []
paren = 0
brace = 0
for ch in s:
if ch == "(":
paren += 1
elif ch == ")":
paren = max(0, paren - 1)
elif ch == "{":
brace += 1
elif ch == "}":
brace = max(0, brace - 1)
if ch == "," and paren == 0 and brace == 0:
token = "".join(buf).strip()
if token:
out.append(token)
buf = []
continue
buf.append(ch)
token = "".join(buf).strip()
if token:
out.append(token)
return out
def parse_step(self, token: str):
"""
Parse one combo token into a step.
Supported formats (case-insensitive for 'hold'/'wait'):
- Normal press/click: "a", "space", "lmb"
- Hold: "hold(space,500)" or "space{500}" or "space{500ms}"
- Wait: "wait:0.5" (minimum delay before next input)
Returns a dict:
- press: {"input": "a", "hold_ms": None, "wait_ms": None}
- hold: {"input": "space", "hold_ms": 500, "wait_ms": None}
- wait: {"input": None, "hold_ms": None, "wait_ms": 500}
"""
t = (token or "").strip()
if not t:
return None
tl = t.lower()
# wait:<duration>
if tl.startswith("wait:"):
dur = tl[len("wait:"):].strip()
wait_ms = self._parse_duration(dur)
if wait_ms is not None:
return {"input": None, "hold_ms": None, "wait_ms": wait_ms}
if tl.startswith("hold(") and tl.endswith(")"):
inner = tl[len("hold("):-1]
parts = [p.strip() for p in inner.split(",", 1)]
if len(parts) == 2 and parts[0] and parts[1]:
hold_ms = self._parse_duration(parts[1])
if hold_ms is not None:
return {"input": parts[0], "hold_ms": hold_ms, "wait_ms": None}
if "{" in tl and tl.endswith("}"):
base, rest = tl.split("{", 1)
ms_str = rest[:-1].replace("ms", "").strip()
base = base.strip()
if base:
hold_ms = self._parse_duration(ms_str)
if hold_ms is not None:
return {"input": base, "hold_ms": hold_ms, "wait_ms": None}
return {"input": tl, "hold_ms": None, "wait_ms": None}
def calc_min_combo_time_ms(self, steps):
"""
Minimum possible completion time for a combo (best case).
- press steps: 0ms
- wait steps: +wait_ms
- hold steps: +hold_ms
"""
total = 0
for s in steps or []:
if not isinstance(s, dict):
continue
w = s.get("wait_ms")
h = s.get("hold_ms")
if w is not None:
total += int(w)
if h is not None:
total += int(h)
return max(0, int(total))
def _format_ms(self, ms: int):
ms = int(ms)
if ms % 1000 == 0:
return f"{ms//1000:d}s ({ms}ms)"
return f"{ms/1000.0:.3g}s ({ms}ms)"
def update_min_time_label(self, steps):
if not steps:
self.lbl_min_time.config(text="Fastest possible: —")
return
min_ms = self.calc_min_combo_time_ms(steps)
self.lbl_min_time.config(text=f"Fastest possible: {self._format_ms(min_ms)}")
def update_min_time_from_editor(self):
keys_str = self.entry_keys.get().strip()
if not keys_str:
self.update_min_time_label([])
return
tokens = [k.strip().lower() for k in self.split_inputs(keys_str) if k.strip()]
steps = []
for t in tokens:
s = self.parse_step(t)
if s:
steps.append(s)
self.update_min_time_label(steps)
def _parse_duration(self, raw: str):
token = raw.lower().strip()
if not token:
return None
# Accept suffixes "ms" for milliseconds and "s" for seconds.
if token.endswith("ms"):
token = token[:-2].strip()
multiplier = 1
elif token.endswith("s"):
token = token[:-1].strip()
multiplier = 1000
else:
multiplier = 1000 if "." in token else 1
try:
value = float(token)
except ValueError:
return None
millis = value * multiplier
if millis <= 0:
return None
return int(millis)
def _active_step(self):
if 0 <= self.current_index < len(self.active_combo_steps):
return self.active_combo_steps[self.current_index]
return None
def _format_hold_requirement(self, hold_ms: int):
if hold_ms is None:
return ""
if hold_ms % 1000 == 0:
return f"{hold_ms // 1000:d}s"
return f"{hold_ms / 1000.0:.3g}s"
def _format_wait_requirement(self, wait_ms: int):
return self._format_hold_requirement(wait_ms)
def _insert_attempt_separator(self):
self.attempt_counter += 1
name = self.active_combo_name or "Combo"
label = f"—— {name} | Attempt {self.attempt_counter} ——"
self.tree.insert("", "end", values=(label, "", ""), tags=("sep",))
self.tree.yview_moveto(1)
def _is_combo_ender(self, input_name: str) -> bool:
return input_name in self.combo_enders
def _ender_grace_for(self, input_name: str) -> int:
"""Return grace in ms for this ender key. Missing keys => 0 (no grace)."""
try:
return int(self.combo_enders.get(input_name, 0))
except Exception:
return 0
def _within_ender_grace(self, input_name: str) -> bool:
grace_ms = self._ender_grace_for(input_name)
if not grace_ms or grace_ms <= 0:
return False
if not self.last_input_time:
return False
now = time.perf_counter()
return ((now - self.last_input_time) * 1000) <= float(grace_ms)
def _should_ignore_ender_miss(self, input_name: str) -> bool:
"""
Grace windows are meant for "spam" of the most recent correct input (e.g. pressing 2 twice),
not for random wrong buttons (e.g. pressing 2 after hitting f).
"""
return (input_name == self.last_success_input) and self._within_ender_grace(input_name)
def _start_hold(self, input_name: str, required_ms: int, now: float):
self.hold_in_progress = True
self.hold_expected_input = input_name
self.hold_started_at = now
self.hold_required_ms = required_ms
# Insert a visible row immediately so it's clear we're holding.
req_s = self._format_hold_requirement(required_ms)
self.hold_row_id = self.tree.insert(
"",
"end",
values=(f"{input_name} (hold ≥ {req_s})", "HOLDING", "..."),
)
self.tree.yview_moveto(1)
self.update_status(
f"Holding '{input_name.upper()}' (≥ {req_s}). Release OR press next input to continue...",
"green",
)
def _reset_hold_state(self):
self.hold_in_progress = False
self.hold_expected_input = None
self.hold_started_at = 0.0
self.hold_row_id = None
self.hold_required_ms = None
def _start_wait(self, required_ms: int):
# Wait timing starts from the last successful step time.
self.wait_in_progress = True
self.wait_started_at = float(self.last_input_time or time.perf_counter())
self.wait_required_ms = required_ms
self.wait_until = self.wait_started_at + (required_ms / 1000.0)
req_s = self._format_wait_requirement(required_ms)
self.wait_row_id = self.tree.insert(
"",
"end",
values=(f"wait (≥ {req_s})", "WAITING", "..."),
tags=("sep",),
)
self.tree.yview_moveto(1)
self.update_status(f"Waiting ≥ {req_s}...", "green")
def _reset_wait_state(self):
self.wait_in_progress = False
self.wait_started_at = 0.0
self.wait_until = 0.0
self.wait_row_id = None
self.wait_required_ms = None
def _complete_wait(self, now: float, *, fail: bool, reason: str | None = None):
"""
Complete (or fail) the current wait step and advance/reset state.
When successful, advances current_index by 1.
"""
required_ms = int(self.wait_required_ms or 0)
waited_ms = max(0.0, (now - self.wait_started_at) * 1000)
req_s = self._format_wait_requirement(required_ms) if required_ms else "?"
label = f"wait (≥ {req_s}, {waited_ms:.0f}ms)"
total_ms = (now - self.start_time) * 1000 if self.start_time else 0.0
if fail:
if reason:
label += f" [{reason}]"
if self.wait_row_id:
self.tree.item(self.wait_row_id, values=(label, "FAIL", "FAIL"))
else:
self.tree.insert("", "end", values=(label, "FAIL", "FAIL"))
self.tree.yview_moveto(1)
self.update_status("Combo Dropped (Too Early)", "red")
self.current_index = 0
self._reset_hold_state()
self._reset_wait_state()
return False
# success
split_ms = (now - self.last_input_time) * 1000 if self.last_input_time else 0.0
if self.wait_row_id:
self.tree.item(self.wait_row_id, values=(label, f"{split_ms:.1f}", f"{total_ms:.1f}"))
else:
self.record_hit(label, split_ms, total_ms)
self.tree.yview_moveto(1)
self.last_input_time = now
# wait is not a real input; do not change last_success_input
self.current_index += 1
self._reset_wait_state()
return True
def _maybe_start_wait_step(self):
step = self._active_step()
if not step:
return
wait_ms = step.get("wait_ms")
if wait_ms is not None:
# Only start once per wait step.
if not self.wait_in_progress:
self._start_wait(int(wait_ms))
def _complete_hold(self, now: float, *, auto: bool):
"""
Complete the current hold-step (either via release or auto-complete when next input happens).
Assumes we're currently on a hold-step and hold_in_progress is True.
"""
step = self._active_step()
if not step or step["hold_ms"] is None:
return False
target_input = step["input"]
target_hold_ms = step["hold_ms"]
held_ms = (now - self.hold_started_at) * 1000
ok = held_ms >= float(target_hold_ms)
req_s = self._format_hold_requirement(target_hold_ms)
split_ms = (now - self.last_input_time) * 1000 if self.current_index != 0 else 0.0
total_ms = (now - self.start_time) * 1000
label = f"{target_input} (hold ≥ {req_s}, {held_ms:.0f}ms)"
if auto:
label += " [auto]"
if ok:
if self.hold_row_id:
self.tree.item(self.hold_row_id, values=(label, f"{split_ms:.1f}", f"{total_ms:.1f}"))
self.tree.yview_moveto(1)
else:
self.record_hit(label, split_ms, total_ms)
self.last_input_time = now
self.last_success_input = target_input
self.current_index += 1
# If next step is a wait, start it immediately for clarity.
self._maybe_start_wait_step()
if self.current_index >= len(self.active_combo_steps):
self.update_status(f"Combo '{self.active_combo_name}' Complete!", "green")
self.current_index = 0
else:
if self.hold_row_id:
self.tree.item(self.hold_row_id, values=(label, "FAIL", "FAIL"))
self.tree.yview_moveto(1)
else:
self.tree.insert("", "end", values=(label, "FAIL", "FAIL"))
self.update_status("Combo Dropped (Hold Too Short)", "red")
self.tree.yview_moveto(1)
self.current_index = 0
self._reset_hold_state()
return ok
def process_press(self, input_name):
"""Handle press/down events for both Mouse and Keyboard"""
self.currently_pressed.add(input_name)
if not self.active_combo_steps:
return
# If we're currently on a hold-step and the hold requirement is already satisfied,
# allow the user to press the next input without releasing the held key.
# This is common in games (hold-through inputs).
while True:
step = self._active_step()
if not step:
return
target_input = step["input"]
target_hold_ms = step["hold_ms"]
target_wait_ms = step.get("wait_ms")
# WAIT step handling
if target_wait_ms is not None:
self._maybe_start_wait_step()
now = time.perf_counter()
if now < self.wait_until:
# Too early: only ender presses fail; everything else is ignored.
if self._is_combo_ender(input_name):
self._complete_wait(now, fail=True, reason=f"{input_name} too early")
return
# Wait satisfied: complete and then re-evaluate this same input against next step.
self._complete_wait(now, fail=False)
continue
if target_hold_ms is not None and self.hold_in_progress and self.hold_expected_input == target_input:
# Ignore repeated press events for the same held input (Windows key-repeat).
if input_name == target_input:
return
now = time.perf_counter()
held_ms = (now - self.hold_started_at) * 1000
if held_ms >= float(target_hold_ms):
# Auto-complete the hold and then re-evaluate this same input against the next step.
self._complete_hold(now, auto=True)
continue
break
# 1. Start of Combo
if self.current_index == 0:
if input_name == target_input:
now = time.perf_counter()
self._insert_attempt_separator()
self.start_time = now
self.last_input_time = now
if target_hold_ms is None:
self.record_hit(input_name, 0, 0)
self.current_index += 1
self._maybe_start_wait_step()
self.last_success_input = input_name
self.update_status("Recording...", "green")
else:
self._start_hold(input_name, target_hold_ms, now)
else:
pass # Ignore random inputs before start
# 2. During Combo
else:
current_time = time.perf_counter()
if input_name == target_input:
if target_hold_ms is None:
# HIT (press)
split_ms = (current_time - self.last_input_time) * 1000
total_ms = (current_time - self.start_time) * 1000
self.record_hit(input_name, split_ms, total_ms)
self.last_input_time = current_time
self.last_success_input = input_name
self.current_index += 1
self._maybe_start_wait_step()
if self.current_index >= len(self.active_combo_steps):
self.update_status(f"Combo '{self.active_combo_name}' Complete!", "green")
self.current_index = 0
else:
# Start holding; completion happens on release.
self._start_hold(input_name, target_hold_ms, current_time)
else:
# MISS
if self._is_combo_ender(input_name):
# Allow spamming this ender key ONLY if it's the last successful input.
if self._should_ignore_ender_miss(input_name):
return
self.tree.insert("", "end", values=(f"{input_name} (Exp: {target_input})", "FAIL", "FAIL"))
self.update_status("Combo Dropped (Wrong Input)", "red")
self.tree.yview_moveto(1)
self.current_index = 0
self._reset_hold_state()
else:
# Non-ender inputs are ignored (useful for games where many keys do nothing).
return
def process_release(self, input_name):
"""Handle key/button release events for hold-steps."""
self.currently_pressed.discard(input_name)
if not self.active_combo_steps:
return
step = self._active_step()
if not step:
return
target_input = step["input"]
target_hold_ms = step["hold_ms"]
if target_hold_ms is None:
return
if not self.hold_in_progress or self.hold_expected_input != input_name:
return
if input_name != target_input:
return
now = time.perf_counter()
self._complete_hold(now, auto=False)
# --- Event Callbacks ---
def on_key_press(self, key):
key_name = self.normalize_key(key)
# Schedule the logic on the main thread (Tkinter isn't thread-safe)
self.root.after(0, lambda: self.process_press(key_name))
def on_key_release(self, key):
key_name = self.normalize_key(key)
self.root.after(0, lambda: self.process_release(key_name))
def on_mouse_click(self, x, y, button, pressed):
btn_name = self.normalize_mouse(button)
if pressed:
self.root.after(0, lambda: self.process_press(btn_name))
else:
self.root.after(0, lambda: self.process_release(btn_name))
# --- GUI Helpers ---
def new_combo(self):
"""Clear editor fields and deselect active combo (start a new preset)."""
self.active_combo_name = None
self.active_combo_tokens = []
self.active_combo_steps = []
self.combo_selector.set("")
self.entry_name.delete(0, tk.END)
self.entry_keys.delete(0, tk.END)
self.reset_tracking()
self.lbl_status.config(text="Status: Select a combo to start", fg="gray")
self.update_min_time_label([])
def save_combo(self):
# Enders are global settings; apply+persist them when saving/updating combos.
self.apply_enders(show_status=False)
name = self.entry_name.get().strip()
keys_str = self.entry_keys.get().strip()
if not name or not keys_str:
messagebox.showerror("Error", "Please fill in Name and Inputs.")
return
# Parse inputs (comma-separated, but respect hold(e,0.2) commas)
input_list = [k.strip().lower() for k in self.split_inputs(keys_str) if k.strip()]
if not input_list:
messagebox.showerror("Error", "Please provide at least one input.")
return
# If editing an existing combo and the name changed, treat this as rename.
old_name = self.active_combo_name if self.active_combo_name in self.combos else None
if old_name and name != old_name:
if name in self.combos:
ok = messagebox.askyesno(
"Overwrite?",
f"A combo named '{name}' already exists.\nOverwrite it?",
)
if not ok:
return
# Overwrite existing target name; delete old key afterwards.
self.combos[name] = input_list
if old_name != name and old_name in self.combos:
del self.combos[old_name]
else:
# Creating new OR updating an existing combo with same name
if name in self.combos and not old_name:
ok = messagebox.askyesno(
"Overwrite?",
f"A combo named '{name}' already exists.\nOverwrite it?",
)
if not ok:
return
self.combos[name] = input_list
self.combo_selector['values'] = list(self.combos.keys())
self.combo_selector.set(name)
self.set_active_combo(None)
self.save_combos()
def set_active_combo(self, event):
name = self.combo_selector.get()
if name in self.combos:
self.active_combo_name = name
self.active_combo_tokens = self.combos[name]
steps = []
for t in self.active_combo_tokens:
s = self.parse_step(t)
if s:
steps.append(s)
self.active_combo_steps = steps
self.update_min_time_label(self.active_combo_steps)
# Populate editor fields for easy editing.
self.entry_name.delete(0, tk.END)
self.entry_name.insert(0, name)
self.entry_keys.delete(0, tk.END)
self.entry_keys.insert(0, ", ".join(self.active_combo_tokens))
self.reset_tracking()
first = self.active_combo_steps[0]
start_key = first["input"].upper()
if first["hold_ms"] is None:
self.lbl_status.config(text=f"Ready! Press '{start_key}' to start.", fg="blue")
else:
self.lbl_status.config(text=f"Ready! Hold '{start_key}' for {first['hold_ms']}ms to start.", fg="blue")
def delete_active_combo(self):
name = self.combo_selector.get().strip()
if not name or name not in self.combos:
messagebox.showerror("Error", "Select a combo to delete.")
return
ok = messagebox.askyesno("Delete Combo?", f"Delete '{name}'? This cannot be undone.")
if not ok:
return
del self.combos[name]
self.combo_selector["values"] = list(self.combos.keys())
self.save_combos()
self.new_combo()
def reset_tracking(self):
self.current_index = 0
self.start_time = 0
self.last_input_time = 0
self.attempt_counter = 0
self.last_success_input = None
self._reset_hold_state()
self._reset_wait_state()
for item in self.tree.get_children():
self.tree.delete(item)
def record_hit(self, name, split, total):
self.tree.insert("", "end", values=(name, f"{split:.1f}", f"{total:.1f}"))
self.tree.yview_moveto(1)
def update_status(self, text, color):
self.lbl_status.config(text=text, fg=color)
# --- Persistence Helpers ---
def _get_data_dir(self) -> Path:
"""
Save alongside the script/exe (same directory as the app).
"""
if getattr(sys, "frozen", False):
# PyInstaller exe
return Path(sys.executable).resolve().parent
return Path(__file__).resolve().parent
def load_combos(self):
try:
if not self.save_path.exists():
return
data = json.loads(self.save_path.read_text(encoding="utf-8"))
combos = data.get("combos", {})
if isinstance(combos, dict):
# sanitize to {str: [str, ...]}
sanitized = {}
for name, seq in combos.items():
if not isinstance(name, str) or not isinstance(seq, list):
continue
sanitized[name] = [str(x).strip().lower() for x in seq if str(x).strip()]
self.combos = sanitized
self.combo_selector["values"] = list(self.combos.keys())
enders = data.get("combo_enders", {})
parsed = {}
if isinstance(enders, dict):
for k, v in enders.items():
key = str(k).strip().lower()
if not key:
continue
try:
ms = int(float(v))
except Exception:
ms = 0
parsed[key] = max(0, ms)
elif isinstance(enders, list):
for x in enders:
key = str(x).strip().lower()
if key:
parsed[key] = 0
self.combo_enders = parsed
# Populate the settings UI
self.entry_enders.delete(0, tk.END)
if self.combo_enders:
parts = []
for k in sorted(self.combo_enders.keys()):
ms = int(self.combo_enders[k])
if ms > 0:
parts.append(f"{k}:{ms/1000.0:.3g}")
else:
parts.append(k)
self.entry_enders.insert(0, ", ".join(parts))
last_active = data.get("last_active_combo")
if last_active in self.combos:
self.combo_selector.set(last_active)
self.set_active_combo(None)
except Exception:
# If the save file is corrupt or unreadable, just start fresh.
self.combos = {}
self.combo_selector["values"] = []
def save_combos(self):
try:
self.data_dir.mkdir(parents=True, exist_ok=True)
payload = {
"version": 1,
"last_active_combo": self.active_combo_name,
"combos": self.combos,
"combo_enders": dict(self.combo_enders),
}
self.save_path.write_text(json.dumps(payload, indent=2), encoding="utf-8")
except Exception:
# Saving is best-effort; don't crash the UI on close.
pass
def apply_enders(self, show_status: bool = True):
"""
Read the combo enders list from the UI.
Format: q:0.2,e:0.2,r:5.0,lmb,1:1.0,2:1.0,3:1.0, space
- If a timing is omitted for a key, it gets 0 seconds (no grace).
"""
raw = self.entry_enders.get().strip()
if not raw:
self.combo_enders = {}
self.save_combos()
if show_status:
self.update_status("Combo enders cleared (no keys will end the combo).", "gray")
return
parsed = {}
for token in self.split_inputs(raw):
t = token.strip()
if not t:
continue
if ":" in t:
k, v = t.split(":", 1)
key = k.strip().lower()
if not key:
continue
try:
sec = float(v.strip())
except ValueError:
messagebox.showerror("Error", f"Invalid timing for '{key}'. Use seconds, e.g. {key}:0.2")
return
parsed[key] = max(0, int(sec * 1000))
else:
key = t.strip().lower()
if key:
parsed[key] = 0
self.combo_enders = parsed
self.save_combos()
if show_status:
self.update_status("Combo enders applied.", "gray")
def on_close(self):
# Stop listeners first (they run off-thread) to avoid input callbacks after teardown.
try:
self.key_listener.stop()
except Exception:
pass
try:
self.mouse_listener.stop()
except Exception:
pass
# Persist ender edits even if the user didn't click Save/Update.
try:
self.apply_enders(show_status=False)
except Exception:
pass
self.save_combos()
self.root.destroy()
if __name__ == "__main__":
root = tk.Tk()
app = ComboTrackerApp(root)
root.mainloop()