Compare commits
2 Commits
8463eb28df
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
6c26e99dcf | ||
|
|
a701c0abd3 |
67
src/main.pyw
67
src/main.pyw
@@ -12,6 +12,8 @@ from config import load_config, save_config, get_shortcut
|
|||||||
from startup import add_to_startup, remove_from_startup, is_in_startup
|
from startup import add_to_startup, remove_from_startup, is_in_startup
|
||||||
import os
|
import os
|
||||||
import atexit
|
import atexit
|
||||||
|
import sys
|
||||||
|
import tkinter as tk
|
||||||
|
|
||||||
def convert_en_characters(input_str):
|
def convert_en_characters(input_str):
|
||||||
"""
|
"""
|
||||||
@@ -137,6 +139,68 @@ def quit_app(icon, item):
|
|||||||
import os
|
import os
|
||||||
os._exit(0)
|
os._exit(0)
|
||||||
|
|
||||||
|
def restart_app(icon, item):
|
||||||
|
icon.stop()
|
||||||
|
# Unhook all keyboard hooks before restarting
|
||||||
|
keyboard.unhook_all()
|
||||||
|
cleanup_lock()
|
||||||
|
import subprocess
|
||||||
|
python = sys.executable
|
||||||
|
script = os.path.abspath(__file__)
|
||||||
|
subprocess.Popen([python, script])
|
||||||
|
os._exit(0)
|
||||||
|
|
||||||
|
def change_shortcut_dialog():
|
||||||
|
"""Show a dialog to capture a new keyboard shortcut."""
|
||||||
|
root = tk.Tk()
|
||||||
|
root.title("Change Shortcut")
|
||||||
|
root.geometry("400x200")
|
||||||
|
root.resizable(False, False)
|
||||||
|
|
||||||
|
# Center the window
|
||||||
|
root.update_idletasks()
|
||||||
|
x = (root.winfo_screenwidth() // 2) - (400 // 2)
|
||||||
|
y = (root.winfo_screenheight() // 2) - (200 // 2)
|
||||||
|
root.geometry(f"400x200+{x}+{y}")
|
||||||
|
|
||||||
|
captured_shortcut = {"value": None}
|
||||||
|
|
||||||
|
label = tk.Label(root, text="Press the key combination you want to use\nas a shortcut, then press ESC",
|
||||||
|
font=("Arial", 12), pady=20)
|
||||||
|
label.pack()
|
||||||
|
|
||||||
|
shortcut_label = tk.Label(root, text="Waiting for input...", font=("Arial", 14, "bold"), fg="blue")
|
||||||
|
shortcut_label.pack(pady=10)
|
||||||
|
|
||||||
|
def capture_shortcut():
|
||||||
|
try:
|
||||||
|
# Unregister current hotkey before capturing new one
|
||||||
|
keyboard.unhook_all_hotkeys()
|
||||||
|
shortcut = keyboard.read_hotkey(suppress=False)
|
||||||
|
captured_shortcut["value"] = shortcut
|
||||||
|
shortcut_label.config(text=f"Captured: {shortcut}")
|
||||||
|
root.after(1000, root.destroy)
|
||||||
|
except Exception as e:
|
||||||
|
shortcut_label.config(text=f"Error: {str(e)}", fg="red")
|
||||||
|
root.after(2000, root.destroy)
|
||||||
|
|
||||||
|
# Start capturing in a separate thread
|
||||||
|
capture_thread = threading.Thread(target=capture_shortcut, daemon=True)
|
||||||
|
capture_thread.start()
|
||||||
|
|
||||||
|
root.mainloop()
|
||||||
|
return captured_shortcut["value"]
|
||||||
|
|
||||||
|
def on_change_shortcut(icon, item):
|
||||||
|
"""Handle shortcut change request."""
|
||||||
|
new_shortcut = change_shortcut_dialog()
|
||||||
|
if new_shortcut:
|
||||||
|
config = load_config()
|
||||||
|
config["shortcut"] = new_shortcut
|
||||||
|
save_config(config)
|
||||||
|
# Always restart after shortcut change attempt to ensure hotkeys are properly re-registered
|
||||||
|
restart_app(icon, item)
|
||||||
|
|
||||||
def setup_tray(config):
|
def setup_tray(config):
|
||||||
def on_toggle_lang_change(icon, item):
|
def on_toggle_lang_change(icon, item):
|
||||||
config["lang_change"] = not item.checked
|
config["lang_change"] = not item.checked
|
||||||
@@ -152,7 +216,7 @@ def setup_tray(config):
|
|||||||
|
|
||||||
image = Image.open("assets/icon.png")
|
image = Image.open("assets/icon.png")
|
||||||
menu = pystray.Menu(
|
menu = pystray.Menu(
|
||||||
pystray.MenuItem(f'Current Shortcut: {get_shortcut()}',lambda icon, item: None),
|
pystray.MenuItem(f'Shortcut: {get_shortcut()} (click to change)', on_change_shortcut),
|
||||||
pystray.MenuItem(
|
pystray.MenuItem(
|
||||||
'change keyboard language layout after transformation',
|
'change keyboard language layout after transformation',
|
||||||
on_toggle_lang_change,
|
on_toggle_lang_change,
|
||||||
@@ -163,6 +227,7 @@ def setup_tray(config):
|
|||||||
on_toggle_startup,
|
on_toggle_startup,
|
||||||
checked=lambda item: config["run_on_startup"]
|
checked=lambda item: config["run_on_startup"]
|
||||||
),
|
),
|
||||||
|
pystray.MenuItem('Restart', restart_app),
|
||||||
pystray.MenuItem('Quit', quit_app)
|
pystray.MenuItem('Quit', quit_app)
|
||||||
)
|
)
|
||||||
icon = pystray.Icon("oopswronglang", image, "OopsWrongLang", menu)
|
icon = pystray.Icon("oopswronglang", image, "OopsWrongLang", menu)
|
||||||
|
|||||||
Reference in New Issue
Block a user