forked from ILikeAI/AlwaysReddy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhotkey_config_GUI.py
130 lines (113 loc) · 4.69 KB
/
hotkey_config_GUI.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
import sys
import tkinter as tk
from tkinter import ttk
import re
if sys.platform.startswith('linux'):
from pynput.keyboard import Key
from pynput import keyboard
else:
import keyboard
CONFIG_FILE_PATH = "config.py"
def load_hotkeys():
hotkeys = {}
try:
with open(CONFIG_FILE_PATH, "r") as file:
lines = file.readlines()
for line in lines:
if re.match(r"^\w+_HOTKEY = '.+'$", line):
key, value = line.split(" = ")
hotkeys[key.strip()] = value.strip().strip("'")
except FileNotFoundError:
pass # Handle case where config file does not exist yet
return hotkeys
def save_hotkeys(hotkeys):
with open(CONFIG_FILE_PATH, "r") as file:
lines = file.readlines()
with open(CONFIG_FILE_PATH, "w") as file:
for line in lines:
match = re.match(r"^(\w+_HOTKEY) = '.*'$", line)
if match and match.group(1) in hotkeys:
file.write(f"{match.group(1)} = '{hotkeys[match.group(1)]}'\n")
else:
file.write(line)
def start_listening_for_hotkey_linux(root, button, label, hotkey_name, hotkeys):
current_keys = set()
def on_key_press(key):
if key == Key.ctrl_l or key == Key.ctrl_r:
current_keys.add("ctrl")
elif isinstance(key, Key):
current_keys.add(key.name)
else:
current_keys.add(key.char.lower())
def on_key_release(key):
if key == Key.ctrl_l or key == Key.ctrl_r:
if "ctrl" in current_keys:
hotkey_str = '+'.join(sorted(current_keys))
label.config(text=f"{hotkey_name}: {hotkey_str}")
hotkeys[hotkey_name] = hotkey_str
save_hotkeys(hotkeys)
listener.stop()
button.config(text="Set", state="normal")
elif isinstance(key, Key):
if key.name in current_keys:
hotkey_str = '+'.join(sorted(current_keys))
label.config(text=f"{hotkey_name}: {hotkey_str}")
hotkeys[hotkey_name] = hotkey_str
save_hotkeys(hotkeys)
listener.stop()
button.config(text="Set", state="normal")
else:
if key.char.lower() in current_keys:
hotkey_str = '+'.join(sorted(current_keys))
label.config(text=f"{hotkey_name}: {hotkey_str}")
hotkeys[hotkey_name] = hotkey_str
save_hotkeys(hotkeys)
listener.stop()
button.config(text="Set", state="normal")
button.config(text="Listening...", state="disabled")
listener = keyboard.Listener(on_press=on_key_press, on_release=on_key_release)
listener.start()
def start_listening_for_hotkey_windows(root, button, label, hotkey_name, hotkeys):
current_keys = set()
def on_key_event(event):
if event.event_type == 'down':
if event.name.isalpha():
current_keys.add(event.name.lower())
else:
current_keys.add(event.name)
elif event.event_type == 'up' and event.name in current_keys:
hotkey_str = '+'.join(sorted(current_keys))
label.config(text=f"{hotkey_name}: {hotkey_str}")
hotkeys[hotkey_name] = hotkey_str
save_hotkeys(hotkeys)
keyboard.unhook_all()
button.config(text="Set", state="normal")
button.config(text="Listening...", state="disabled")
keyboard.hook(on_key_event)
def load_interface(root, hotkeys):
style = ttk.Style()
style.theme_use('clam')
style.configure('TButton', background='#88C0D0', foreground='white', borderwidth=1)
style.map('TButton',
background=[('active', '#81A1C1')],
foreground=[('active', 'white')])
for widget in root.winfo_children():
widget.destroy()
for hotkey_name, hotkey_value in hotkeys.items():
frame = ttk.Frame(root, padding=5)
frame.pack(pady=5, padx=10, fill=tk.X)
label = ttk.Label(frame, text=f"{hotkey_name}: {hotkey_value}")
label.pack(side=tk.LEFT, padx=10)
button = ttk.Button(frame, text="Set", style='TButton')
button.pack(side=tk.LEFT, padx=10)
start_listening_for_hotkey = start_listening_for_hotkey_linux if sys.platform.startswith('linux') else start_listening_for_hotkey_windows
button['command'] = lambda b=button, l=label, n=hotkey_name: start_listening_for_hotkey(root, b, l, n, hotkeys)
def main():
root = tk.Tk()
root.title("Hotkey Configuration")
root.configure(bg='#ECEFF4')
hotkeys = load_hotkeys()
load_interface(root, hotkeys)
root.mainloop()
if __name__ == "__main__":
main()