diff --git a/src/main.pyw b/src/main.pyw index 543c74f..a375e4a 100644 --- a/src/main.pyw +++ b/src/main.pyw @@ -13,6 +13,7 @@ from startup import add_to_startup, remove_from_startup, is_in_startup import os import atexit import sys +import tkinter as tk def convert_en_characters(input_str): """ @@ -140,6 +141,8 @@ def quit_app(icon, item): def restart_app(icon, item): icon.stop() + # Unhook all keyboard hooks before restarting + keyboard.unhook_all() cleanup_lock() import subprocess python = sys.executable @@ -147,6 +150,57 @@ def restart_app(icon, item): 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 on_toggle_lang_change(icon, item): config["lang_change"] = not item.checked @@ -162,7 +216,7 @@ def setup_tray(config): image = Image.open("assets/icon.png") 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( 'change keyboard language layout after transformation', on_toggle_lang_change,