-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclipboard-typer.py
749 lines (604 loc) · 28.7 KB
/
clipboard-typer.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
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
import keyboard
import pyperclip
import time
import threading
import random
import tkinter as tk
from tkinter import ttk
import json
import os
class ClipboardTyper:
def __init__(self):
self.typing = False
# Default settings - these are fallback values only
self.settings = {
'min_delay': 0.05,
'max_delay': 0.15,
'start_delay': 0.5,
'hotkey': 'ctrl+shift+t',
'stop_key': 'esc',
'theme': 'light'
}
print("Starting ClipboardTyper...")
# Theme colors
self.themes = {
'light': {
'bg': '#f0f0f0',
'fg': '#000000',
'button': '#e0e0e0',
'highlight': '#0078d7',
'frame': '#e9e9e9'
},
'dark': {
'bg': '#2d2d2d',
'fg': '#ffffff',
'button': '#3d3d3d',
'highlight': '#0078d7',
'frame': '#383838'
},
'hacker': {
'bg': '#0a0a0a',
'fg': '#00ff41', # Matrix green
'button': '#222222',
'highlight': '#9600ff', # Cyberpunk purple
'frame': '#1a1a1a'
}
}
# Load settings BEFORE creating GUI
self.load_settings()
# Create the GUI - this creates the variables and entry fields
self.create_gui()
# Register hotkeys AFTER GUI is created and populated
self.register_hotkeys()
def load_settings(self):
"""Load settings from a JSON file if it exists."""
settings_file = 'clipboard_typer_settings.json'
if os.path.exists(settings_file):
try:
with open(settings_file, 'r') as f:
loaded_settings = json.load(f)
print(f"Settings loaded from file: {loaded_settings}")
# Check for required keys
required_keys = ['hotkey', 'stop_key', 'min_delay', 'max_delay']
missing_keys = [key for key in required_keys if key not in loaded_settings]
if missing_keys:
print(f"Warning: Missing required keys in settings file: {missing_keys}")
else:
# Update settings
self.settings.update(loaded_settings)
print(f"Applied settings: {self.settings}")
except Exception as e:
print(f"Error loading settings from {settings_file}: {e}")
else:
print(f"Settings file {settings_file} not found. Using defaults: {self.settings}")
def register_hotkeys(self):
"""Register hotkeys with proper error handling."""
# First, completely unhook ALL keyboard hooks
try:
keyboard.unhook_all()
print("Unhooked all keyboard hooks for clean registration")
except Exception as e:
print(f"Error unhooking all keys: {e}")
# Now register the hotkeys
try:
print(f"Registering main hotkey: {self.settings['hotkey']}")
# Make sure hotkey is valid
if not self.settings['hotkey'] or '+' not in self.settings['hotkey']:
print("Invalid hotkey format, defaulting to ctrl+shift+t")
self.settings['hotkey'] = 'ctrl+shift+t'
# Register the hotkey with direct function reference and ensure it's suppressed
keyboard.add_hotkey(self.settings['hotkey'], self.toggle_typing, suppress=True)
print(f"Successfully registered hotkey '{self.settings['hotkey']}' with toggle_typing function")
self.status_var.set(f"Hotkey '{self.settings['hotkey']}' registered")
except Exception as e:
print(f"Error registering hotkey: {e}")
self.status_var.set(f"Error registering hotkey: {str(e)}")
try:
print(f"Registering stop key: {self.settings['stop_key']}")
keyboard.add_hotkey(self.settings['stop_key'], self.stop_typing, suppress=True)
print(f"Successfully registered stop key '{self.settings['stop_key']}' with stop_typing function")
except Exception as e:
print(f"Error registering stop key: {e}")
self.status_var.set(f"Error registering stop key: {str(e)}")
def save_settings(self):
"""Save current settings to a JSON file."""
settings_file = 'clipboard_typer_settings.json'
try:
# Make a copy of settings for writing to file to avoid race conditions
settings_to_save = self.settings.copy()
print(f"Saving settings to file: {settings_to_save}")
with open(settings_file, 'w') as f:
json.dump(settings_to_save, f)
print(f"Settings saved successfully to {settings_file}")
except Exception as e:
print(f"Error saving settings: {e}")
self.status_var.set(f"Error saving settings: {str(e)}")
def create_gui(self):
"""Create the GUI for the application."""
self.root = tk.Tk()
self.root.title("Clipboard Typing Simulator")
self.root.geometry("400x450") # Increase height to ensure buttons are visible
self.root.resizable(False, False)
# Create a style object
self.style = ttk.Style()
# Status variable needs to be created early for error messages
self.status_var = tk.StringVar(value="Ready")
# Main frame
main_frame = ttk.Frame(self.root, padding="20")
main_frame.pack(fill=tk.BOTH, expand=True)
# Initialize theme variable
self.theme_var = tk.StringVar(value=self.settings['theme'])
# Title
title_label = ttk.Label(
main_frame,
text="Clipboard Typing Simulator",
font=("Arial", 16, "bold")
)
title_label.pack(pady=(0, 20))
# Typing speed frame
speed_frame = ttk.LabelFrame(main_frame, text="Typing Speed (seconds)")
speed_frame.pack(fill=tk.X, pady=10)
# Min delay
min_delay_frame = ttk.Frame(speed_frame)
min_delay_frame.pack(fill=tk.X, pady=5)
ttk.Label(min_delay_frame, text="Min Delay:").pack(side=tk.LEFT)
self.min_delay_var = tk.StringVar(value=str(self.settings['min_delay']))
min_delay_entry = ttk.Entry(min_delay_frame, textvariable=self.min_delay_var, width=8)
min_delay_entry.pack(side=tk.LEFT, padx=5)
# Max delay
max_delay_frame = ttk.Frame(speed_frame)
max_delay_frame.pack(fill=tk.X, pady=5)
ttk.Label(max_delay_frame, text="Max Delay:").pack(side=tk.LEFT)
self.max_delay_var = tk.StringVar(value=str(self.settings['max_delay']))
max_delay_entry = ttk.Entry(max_delay_frame, textvariable=self.max_delay_var, width=8)
max_delay_entry.pack(side=tk.LEFT, padx=5)
# Initial delay before typing starts
start_delay_frame = ttk.Frame(speed_frame)
start_delay_frame.pack(fill=tk.X, pady=5)
ttk.Label(start_delay_frame, text="Start Delay:").pack(side=tk.LEFT)
# Add start_delay to settings if it doesn't exist
if 'start_delay' not in self.settings:
self.settings['start_delay'] = 0.5
self.start_delay_var = tk.StringVar(value=str(self.settings['start_delay']))
start_delay_entry = ttk.Entry(start_delay_frame, textvariable=self.start_delay_var, width=8)
start_delay_entry.pack(side=tk.LEFT, padx=5)
# Hotkey frame
hotkey_frame = ttk.LabelFrame(main_frame, text="Hotkeys")
hotkey_frame.pack(fill=tk.X, pady=10)
# Start/stop hotkey
start_hotkey_frame = ttk.Frame(hotkey_frame)
start_hotkey_frame.pack(fill=tk.X, pady=5)
ttk.Label(start_hotkey_frame, text="Start/Stop:").pack(side=tk.LEFT)
# Make sure hotkey string is loaded from settings
self.hotkey_var = tk.StringVar(value=self.settings['hotkey'])
print(f"Setting hotkey entry to: {self.settings['hotkey']}")
self.hotkey_entry = ttk.Entry(start_hotkey_frame, textvariable=self.hotkey_var, width=15)
self.hotkey_entry.pack(side=tk.LEFT, padx=5)
self.record_hotkey_btn = ttk.Button(
start_hotkey_frame,
text="Record",
command=self.record_hotkey
)
self.record_hotkey_btn.pack(side=tk.LEFT, padx=5)
# Emergency stop key
stop_key_frame = ttk.Frame(hotkey_frame)
stop_key_frame.pack(fill=tk.X, pady=5)
ttk.Label(stop_key_frame, text="Emergency Stop:").pack(side=tk.LEFT)
# Make sure stop key is loaded from settings
self.stop_key_var = tk.StringVar(value=self.settings['stop_key'])
print(f"Setting stop key entry to: {self.settings['stop_key']}")
self.stop_key_entry = ttk.Entry(stop_key_frame, textvariable=self.stop_key_var, width=15)
self.stop_key_entry.pack(side=tk.LEFT, padx=5)
self.record_stop_btn = ttk.Button(
stop_key_frame,
text="Record",
command=self.record_stop_key
)
self.record_stop_btn.pack(side=tk.LEFT, padx=5)
# Theme selector frame
theme_frame = ttk.LabelFrame(main_frame, text="Theme")
theme_frame.pack(fill=tk.X, pady=10)
theme_select_frame = ttk.Frame(theme_frame)
theme_select_frame.pack(fill=tk.X, pady=5)
# Create radio buttons for themes
ttk.Radiobutton(
theme_select_frame,
text="Light",
variable=self.theme_var,
value="light",
command=lambda: self.apply_theme("light")
).pack(side=tk.LEFT, padx=5)
ttk.Radiobutton(
theme_select_frame,
text="Dark",
variable=self.theme_var,
value="dark",
command=lambda: self.apply_theme("dark")
).pack(side=tk.LEFT, padx=5)
ttk.Radiobutton(
theme_select_frame,
text="Hacker",
variable=self.theme_var,
value="hacker",
command=lambda: self.apply_theme("hacker")
).pack(side=tk.LEFT, padx=5)
# Action buttons
button_frame = ttk.Frame(main_frame)
button_frame.pack(fill=tk.X, pady=10)
self.save_btn = ttk.Button(
button_frame,
text="Save Settings",
command=self.update_settings
)
self.save_btn.pack(side=tk.LEFT, padx=5, expand=True)
self.start_btn = ttk.Button(
button_frame,
text="Start Typing",
command=self.toggle_typing
)
self.start_btn.pack(side=tk.RIGHT, padx=5, expand=True)
# Status frame
status_frame = ttk.Frame(main_frame)
status_frame.pack(fill=tk.X, pady=10)
status_label = ttk.Label(
status_frame,
textvariable=self.status_var,
font=("Arial", 10)
)
status_label.pack(side=tk.LEFT)
# Info text at the bottom
info_text = f"Press {self.settings['hotkey']} to start/stop typing from clipboard.\nPress {self.settings['stop_key']} for emergency stop."
info_label = ttk.Label(
main_frame,
text=info_text,
justify=tk.CENTER
)
info_label.pack(pady=10)
# Make the window stay on top
self.root.attributes('-topmost', True)
# Apply the theme after all widgets are created
self.apply_theme(self.settings['theme'])
# Set up a protocol for when the window is closed
self.root.protocol("WM_DELETE_WINDOW", self.on_close)
# Update UI periodically to show typing status
self.update_ui()
def record_hotkey(self):
"""Record a new hotkey."""
self.record_hotkey_btn.config(text="Recording...")
self.hotkey_entry.delete(0, tk.END)
self.hotkey_entry.insert(0, "Press keys...")
# Unhook all existing keyboard hooks for clean slate
keyboard.unhook_all()
# Track currently pressed keys
pressed_keys = set()
recording_complete = [False] # Using list for mutable state
# Function to handle key down
def on_key_down(e):
if recording_complete[0]:
return
key = e.name
# Convert 'control' to 'ctrl' for consistency
if key == 'control':
key = 'ctrl'
elif key == 'windows':
key = 'win'
# Add to pressed keys
pressed_keys.add(key)
# Update display
sorted_keys = sorted([k for k in pressed_keys if k in ['ctrl', 'alt', 'shift', 'win']])
other_keys = [k for k in pressed_keys if k not in ['ctrl', 'alt', 'shift', 'win']]
hotkey_str = '+'.join(sorted_keys + other_keys)
self.hotkey_entry.delete(0, tk.END)
self.hotkey_entry.insert(0, hotkey_str)
# Function to handle key up - finalize when a non-modifier key is released
def on_key_up(e):
if recording_complete[0]:
return
key = e.name
# Convert for consistency
if key == 'control':
key = 'ctrl'
elif key == 'windows':
key = 'win'
# Only stop recording if a non-modifier was released
is_modifier = key in ['ctrl', 'alt', 'shift', 'win']
if not is_modifier:
# We have a non-modifier key released, finalize the hotkey
recording_complete[0] = True
sorted_keys = sorted([k for k in pressed_keys if k in ['ctrl', 'alt', 'shift', 'win']])
other_keys = [k for k in pressed_keys if k not in ['ctrl', 'alt', 'shift', 'win']]
# Limit to one non-modifier key
if other_keys:
other_keys = [other_keys[0]]
# Make sure we have at least one modifier and one key
if not sorted_keys:
sorted_keys = ['ctrl'] # Default to ctrl if no modifiers
if not other_keys:
other_keys = ['t'] # Default to 't' if no non-modifier key
final_hotkey = '+'.join(sorted_keys + other_keys)
# Update the UI directly in the Entry widget
self.hotkey_entry.delete(0, tk.END)
self.hotkey_entry.insert(0, final_hotkey)
# Clean up and finalize
finalize_recording(final_hotkey)
def finalize_recording(final_hotkey):
# Clean up
try:
keyboard.unhook_all()
except Exception as e:
print(f"Error unhooking all keys: {e}")
# Re-register main hotkeys - but don't update settings yet
# to avoid overwriting before Save is clicked
try:
print(f"Re-registering original hotkeys: {self.settings['hotkey']} and {self.settings['stop_key']}")
keyboard.add_hotkey(self.settings['hotkey'], self.toggle_typing)
keyboard.add_hotkey(self.settings['stop_key'], self.stop_typing)
except Exception as e:
print(f"Error re-registering hotkeys after recording: {e}")
# Change button text
self.record_hotkey_btn.config(text="Record")
print(f"Recorded hotkey: {final_hotkey}")
# Hook both key down and key up events
keyboard.on_press(on_key_down)
keyboard.on_release(on_key_up)
# Safety timeout (5 seconds)
def timeout_handler():
if not recording_complete[0] and self.record_hotkey_btn.cget('text') == "Recording...":
recording_complete[0] = True
self.record_hotkey_btn.config(text="Record")
# Get whatever we have so far
sorted_keys = sorted([k for k in pressed_keys if k in ['ctrl', 'alt', 'shift', 'win']])
other_keys = [k for k in pressed_keys if k not in ['ctrl', 'alt', 'shift', 'win']]
# Default values if nothing was pressed
if not sorted_keys:
sorted_keys = ['ctrl']
if not other_keys:
other_keys = ['t']
final_hotkey = '+'.join(sorted_keys + other_keys)
# Update the UI
self.hotkey_entry.delete(0, tk.END)
self.hotkey_entry.insert(0, final_hotkey)
finalize_recording(final_hotkey)
self.root.after(5000, timeout_handler)
def record_stop_key(self):
"""Record a new emergency stop key."""
self.record_stop_btn.config(text="Recording...")
self.stop_key_entry.delete(0, tk.END)
self.stop_key_entry.insert(0, "Press key...")
# Unhook all keyboard hooks for clean slate
keyboard.unhook_all()
# Track if we've already processed a key
key_processed = [False] # Using list as a mutable container
# Function to handle key press
def on_key_press(e):
if key_processed[0]:
return False
# Mark as processed to prevent multiple keys
key_processed[0] = True
# Get single key
key = e.name
# Update the Entry widget directly
self.stop_key_entry.delete(0, tk.END)
self.stop_key_entry.insert(0, key)
# Finish recording
finalize_recording(key)
return False # Stop propagation
def finalize_recording(key):
self.record_stop_btn.config(text="Record")
try:
keyboard.unhook_all()
except Exception as e:
print(f"Error unhooking all keys: {e}")
# Re-register main hotkeys
try:
keyboard.add_hotkey(self.settings['hotkey'], self.toggle_typing)
keyboard.add_hotkey(self.settings['stop_key'], self.stop_typing)
except Exception as e:
print(f"Error re-registering hotkeys after stop key recording: {e}")
print(f"Recorded stop key: {key}")
# Hook for a single key press
keyboard.on_press(on_key_press, suppress=True)
# Safety timeout (5 seconds)
def timeout_handler():
if not key_processed[0] and self.record_stop_btn.cget('text') == "Recording...":
key_processed[0] = True
# Default to esc if no key pressed
self.stop_key_entry.delete(0, tk.END)
self.stop_key_entry.insert(0, "esc")
finalize_recording("esc")
self.root.after(5000, timeout_handler)
def update_settings(self):
"""Update settings from the GUI inputs."""
try:
min_delay = float(self.min_delay_var.get())
max_delay = float(self.max_delay_var.get())
start_delay = float(self.start_delay_var.get())
if min_delay < 0 or max_delay < 0 or start_delay < 0:
raise ValueError("Delays cannot be negative")
if min_delay > max_delay:
min_delay, max_delay = max_delay, min_delay
# Directly read values from Entry widgets
hotkey = self.hotkey_entry.get()
stop_key = self.stop_key_entry.get()
theme = self.theme_var.get()
print(f"Reading from UI - hotkey: '{hotkey}', stop_key: '{stop_key}', start_delay: {start_delay}")
# Validate hotkey format
if not hotkey:
hotkey = "ctrl+shift+t" # Default if empty
self.hotkey_entry.delete(0, tk.END)
self.hotkey_entry.insert(0, hotkey)
# Clean up hotkeys to ensure consistent format
if '+' not in hotkey and len(hotkey) > 1:
# This might be a space-separated hotkey, convert to + format
hotkey = hotkey.replace(' ', '+')
self.hotkey_entry.delete(0, tk.END)
self.hotkey_entry.insert(0, hotkey)
# First unhook ALL keyboard listeners to start clean
keyboard.unhook_all()
# Update settings dictionary
self.settings['min_delay'] = min_delay
self.settings['max_delay'] = max_delay
self.settings['start_delay'] = start_delay
self.settings['hotkey'] = hotkey
self.settings['stop_key'] = stop_key
self.settings['theme'] = theme
print(f"Updated settings dictionary: {self.settings}")
# Register new hotkeys with the updated settings
try:
keyboard.add_hotkey(hotkey, self.toggle_typing)
print(f"Successfully registered new hotkey: '{hotkey}'")
self.status_var.set(f"Hotkey '{hotkey}' registered")
except Exception as e:
print(f"Error registering new hotkey: {e}")
self.status_var.set(f"Error registering hotkey: {str(e)}")
try:
keyboard.add_hotkey(stop_key, self.stop_typing)
print(f"Successfully registered new stop key: '{stop_key}'")
except Exception as e:
print(f"Error registering new stop key: {e}")
self.status_var.set(f"Error registering stop key: {str(e)}")
# Save to file AFTER registering hotkeys
self.save_settings()
# Update the info text
info_text = f"Press {hotkey} to start/stop typing from clipboard.\nPress {stop_key} for emergency stop."
# Find the info label by traversing widget hierarchy more reliably
for widget in self.root.winfo_children():
if isinstance(widget, ttk.Frame):
for child in widget.winfo_children():
if isinstance(child, ttk.Label) and child.cget('justify') == tk.CENTER:
child.config(text=info_text)
break
self.status_var.set(f"Settings saved. Hotkey '{hotkey}' registered")
except ValueError as e:
self.status_var.set(f"Error: {str(e)}")
def toggle_typing(self):
"""Toggle the typing simulation on/off."""
print(f"Toggle typing called - current state: {self.typing}")
try:
# Ensure GUI exists before toggling
if self.root and self.root.winfo_exists():
if self.typing:
self.stop_typing()
else:
self.start_typing()
return False # Returning False helps suppress the hotkey in some cases
except Exception as e:
print(f"Error in toggle_typing: {e}")
return False
def start_typing(self):
"""Start typing text from the clipboard."""
print("Starting typing...")
if not self.typing:
clipboard_text = pyperclip.paste()
if clipboard_text:
self.typing = True
self.status_var.set("Typing in progress...")
self.start_btn.config(text="Stop Typing")
# Start typing in a separate thread
threading.Thread(target=self.type_text, args=(clipboard_text,), daemon=True).start()
else:
self.status_var.set("Error: Clipboard is empty")
def stop_typing(self):
"""Stop the typing simulation."""
print("Stopping typing...")
if self.typing:
self.typing = False
self.status_var.set("Typing stopped")
self.start_btn.config(text="Start Typing")
def type_text(self, text):
"""Type out the given text with random delays."""
# Use customizable delay before starting to type
start_delay = self.settings['start_delay']
print(f"Starting typing with {start_delay}s initial delay...")
self.status_var.set(f"Starting in {start_delay}s...")
time.sleep(start_delay)
try:
# Type each character with a random delay
for char in text:
if not self.typing:
break
# Type the character
keyboard.write(char)
# Random delay between characters
delay = random.uniform(self.settings['min_delay'], self.settings['max_delay'])
time.sleep(delay)
except Exception as e:
print(f"Error during typing: {e}")
self.status_var.set(f"Error during typing: {str(e)}")
finally:
# Set typing to False when done
self.typing = False
if self.root and self.root.winfo_exists():
self.status_var.set("Typing completed")
self.start_btn.config(text="Start Typing")
def update_ui(self):
"""Update the UI periodically."""
try:
# Check if root still exists
if self.root and self.root.winfo_exists():
# Update the start button text
if self.typing:
self.start_btn.config(text="Stop Typing")
else:
self.start_btn.config(text="Start Typing")
# Schedule the next update
self.root.after(100, self.update_ui)
except tk.TclError:
# Window was likely destroyed, no need to reschedule
pass
def apply_theme(self, theme_name):
"""Apply the selected theme to the UI."""
if theme_name not in self.themes:
theme_name = 'light' # Fallback to light theme
theme = self.themes[theme_name]
# Update the theme variable
self.theme_var.set(theme_name)
try:
# Configure the root window background
self.root.configure(bg=theme['bg'])
# Configure ttk styles
self.style.configure('TFrame', background=theme['bg'])
self.style.configure('TLabel', background=theme['bg'], foreground=theme['fg'])
self.style.configure('TButton', background=theme['button'])
self.style.configure('TLabelframe', background=theme['bg'])
self.style.configure('TLabelframe.Label', background=theme['bg'], foreground=theme['fg'])
self.style.configure('TRadiobutton', background=theme['bg'], foreground=theme['fg'])
# Update the settings
self.settings['theme'] = theme_name
except tk.TclError as e:
print(f"Error applying theme: {e}")
def on_close(self):
"""Clean up and close the application."""
print("Closing application...")
self.stop_typing()
# Remove all hotkeys and keyboard listeners
try:
print("Unhooking all keyboard hooks...")
keyboard.unhook_all()
except Exception as e:
print(f"Error unhooking all hotkeys: {e}")
# Save settings
print("Saving settings...")
self.save_settings()
# Destroy the GUI
try:
self.root.destroy()
except Exception as e:
print(f"Error destroying root: {e}")
def run(self):
"""Run the main application loop."""
try:
self.root.mainloop()
except Exception as e:
print(f"Error in mainloop: {e}")
def main():
try:
# Create and run the application
app = ClipboardTyper()
# Print hotkey info on startup for debugging
print(f"Starting with hotkey: {app.settings['hotkey']}")
app.run()
except Exception as e:
print(f"Fatal error: {e}")
if __name__ == "__main__":
main()