KeyMapper Python GUI

👤 Sharing: AI
import tkinter as tk
from tkinter import ttk
import json

class KeyMapperApp:
    def __init__(self, master):
        self.master = master
        master.title("KeyMapper: Your Custom Keyboard Shortcuts")

        self.shortcuts = {}
        self.load_shortcuts()

        self.key_label = ttk.Label(master, text="Key Combination:")
        self.key_label.grid(row=0, column=0, padx=5, pady=5, sticky=tk.W)

        self.key_entry = ttk.Entry(master)
        self.key_entry.grid(row=0, column=1, padx=5, pady=5, sticky=tk.W+tk.E)

        self.action_label = ttk.Label(master, text="Action (e.g., open program, website):")
        self.action_label.grid(row=1, column=0, padx=5, pady=5, sticky=tk.W)

        self.action_entry = ttk.Entry(master)
        self.action_entry.grid(row=1, column=1, padx=5, pady=5, sticky=tk.W+tk.E)

        self.add_button = ttk.Button(master, text="Add Shortcut", command=self.add_shortcut)
        self.add_button.grid(row=2, column=0, columnspan=2, pady=10)

        self.shortcuts_list = tk.Listbox(master, width=50, height=10)
        self.shortcuts_list.grid(row=3, column=0, columnspan=2, padx=5, pady=5, sticky=tk.W+tk.E)
        self.update_list()

        self.delete_button = ttk.Button(master, text="Delete Shortcut", command=self.delete_shortcut)
        self.delete_button.grid(row=4, column=0, columnspan=2, pady=10)

        self.details_button = ttk.Button(master, text="Details", command=self.show_details)
        self.details_button.grid(row=5, column=0, columnspan=2, pady=10)

        master.grid_columnconfigure(1, weight=1)  # Make the second column expandable

    def add_shortcut(self):
        key = self.key_entry.get()
        action = self.action_entry.get()
        if key and action:
            self.shortcuts[key] = action
            self.save_shortcuts()
            self.update_list()
            self.key_entry.delete(0, tk.END)
            self.action_entry.delete(0, tk.END)

    def delete_shortcut(self):
        try:
            index = self.shortcuts_list.curselection()[0]
            key = self.shortcuts_list.get(index).split(" -> ")[0]
            del self.shortcuts[key]
            self.save_shortcuts()
            self.update_list()
        except IndexError:
            pass  # No item selected

    def update_list(self):
        self.shortcuts_list.delete(0, tk.END)
        for key, action in self.shortcuts.items():
            self.shortcuts_list.insert(tk.END, f"{key} -> {action}")

    def save_shortcuts(self):
        with open("shortcuts.json", "w") as f:
            json.dump(self.shortcuts, f)

    def load_shortcuts(self):
        try:
            with open("shortcuts.json", "r") as f:
                self.shortcuts = json.load(f)
        except FileNotFoundError:
            self.shortcuts = {}

    def show_details(self):
        details_window = tk.Toplevel(self.master)
        details_window.title("KeyMapper Details")
        details_text = tk.Text(details_window, wrap=tk.WORD)
        details_text.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
        details_text.insert(tk.END, """
KeyMapper allows you to create custom keyboard shortcuts to launch applications, open websites, or perform other actions.  It stores these shortcuts persistently, so they are available each time you run the program.

Key Features:

*   **Easy Shortcut Creation:** Define key combinations and corresponding actions using a simple GUI.
*   **Persistence:** Shortcuts are saved to a JSON file and loaded automatically.
*   **Deletion:** Remove unwanted shortcuts easily.
*   **Versatile Actions:**  Associate shortcuts with any command you can execute from the command line.  For example, opening a specific file, running a Python script, etc.

How to Use:

1.  Enter the desired key combination in the 'Key Combination' field (e.g., Ctrl+Shift+A).
2.  Enter the action to perform in the 'Action' field.  This could be the path to an executable, a URL, or any command-line instruction.
3.  Click 'Add Shortcut'.
4.  To delete a shortcut, select it from the list and click 'Delete Shortcut'.

Potential Improvements:

*   Global hotkey support (requires additional libraries like `keyboard`).
*   More advanced action types (e.g., sending keystrokes to other applications).
*   GUI enhancements for easier shortcut management.
        """)
        details_text.config(state=tk.DISABLED) #Make the text read-only

root = tk.Tk()
app = KeyMapperApp(root)
root.mainloop()
👁️ Viewed: 6

Comments