Advertisement
Python253

custom_hotkey_manager_cli

May 22nd, 2024
796
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.78 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: custom_hotkey_manager_cli.py
  4. # Version: 1.0.0
  5. # Author: Jeoi Reqi
  6.  
  7. """
  8. :: Windows Custom Hotkey Manager CLI Tool ::
  9.  
  10. Description:
  11.    - This script is a command-line interface (CLI) tool designed for managing and creating custom hotkeys in Windows.
  12.    - It allows users to define custom hotkey sequences for launching specific programs and provides a predefined set of useful hotkeys for various Windows actions.
  13.    - By interacting with the script, users can easily create and manage custom hotkeys without needing to access system settings directly.
  14.    - Additionally, the script saves custom hotkeys to a JSON file, ensuring they are persistent across sessions.
  15.  
  16. Requirements:
  17.    - This script is designed primarily for Windows 10 and newer versions, but many functions are compatible with older versions as far back as Windows 7.
  18.      (SEE COMPATIBILITY BELOW FOR FURTHER DETAILS)
  19.    - It requires Python 3.x and the `keyboard` library to be installed on the system.
  20.    - The script requires administrative privileges to set some hotkeys.
  21.  
  22. Usage:
  23.    - Run the script in a terminal or command prompt.
  24.    - Follow the on-screen instructions to create or manage hotkeys.
  25.  
  26. Compatibility:
  27.    - Windows 10 and Windows 11:
  28.        These hotkeys are fully supported.
  29.    - Windows 8 and Windows 8.1:
  30.        Most of these hotkeys are supported, though some features (like virtual desktops) were not introduced until Windows 10.
  31.    - Windows 7:
  32.        Basic hotkeys like opening the start menu, maximizing windows, and taking screenshots are supported,
  33.        but many of the newer features like virtual desktops and some accessibility functions are not present.
  34.  
  35. Note:
  36.    - This script will work on Windows 7 and up, but some features may not be available in older versions.
  37.      If a feature is not present, the script will handle it gracefully by returning to the menu without errors.
  38.  
  39. Functions:
  40.    1. select_program():
  41.       - Opens the Program Files directory in File Explorer, allowing users to select a program.
  42.  
  43.    2. load_hotkeys():
  44.       - Loads predefined and custom hotkeys from a JSON file. If the file does not exist, it initializes with predefined hotkeys.
  45.  
  46.    3. save_hotkeys(hotkeys):
  47.       - Saves the current hotkeys configuration to a JSON file.
  48.  
  49.    4. get_hotkey_input():
  50.       - Captures a custom hotkey sequence from the user. The sequence ends when the 'Esc' key is pressed.
  51.  
  52.    5. create_custom_hotkey():
  53.       - Allows users to create a custom hotkey by selecting a program and assigning a hotkey sequence to it. Saves the custom hotkey to the JSON file.
  54.  
  55. Additional Notes:
  56.    - Ensure that the script is run with appropriate permissions, especially when setting system-wide hotkeys.
  57.    - Users should exercise caution when assigning hotkeys to avoid conflicts with existing system hotkeys.
  58. """
  59.  
  60. import json
  61. import keyboard
  62. import os
  63.  
  64. # Predefined hotkeys
  65. PREDEFINED_HOTKEYS = {
  66.     "open_start_menu": ["win"],
  67.     "open_secret_start_menu": ["win", "x"],
  68.     "cycle_through_taskbar": ["win", "t"],
  69.     "go_to_nth_application": ["win", "[n]"],
  70.     "show_all_running_applications": ["win", "tab"],
  71.     "show_hide_desktop": ["win", "d"],
  72.     "minimize_all_windows": ["ctrl", "m"],
  73.     "temporary_show_desktop": ["win", ","],
  74.     "magnify_screen_content": ["win", "plus"],
  75.     "maximize_window": ["win", "up"],
  76.     "maximize_window_vertically": ["win", "shift", "up"],
  77.     "move_window_to_left_monitor": ["custom"],
  78.     "move_window_to_right_monitor": ["custom"],
  79.     "take_rectangular_screenshot": ["win", "shift", "s"],
  80.     "take_full_screenshot": ["win", "printscreen"],
  81.     "create_new_virtual_desktop": ["win", "ctrl", "d"],
  82.     "move_between_virtual_desktops_left": ["win", "ctrl", "left"],
  83.     "move_between_virtual_desktops_right": ["win", "ctrl", "right"],
  84.     "close_current_virtual_desktop": ["win", "ctrl", "f4"],
  85.     "open_action_center": ["win", "a"],
  86.     "open_search": ["win", "s"],
  87.     "open_new_edge_tab": ["win", "c"],
  88.     "open_windows_settings": ["win", "i"],
  89.     "connect_sidebar": ["win", "k"],
  90.     "use_voice_typing": ["win", "h"],
  91.     "lock_computer": ["win", "l"],
  92.     "lock_screen_orientation": ["win", "o"],
  93.     "open_presentation_sidebar": ["win", "p"],
  94.     "open_ease_of_access_center": ["win", "u"],
  95.     "select_from_clipboard_history": ["win", "v"],
  96.     "set_focus_to_notification_area": ["win", "b"],
  97.     "open_emoji_panel": ["win", "."],
  98.     "start_stop_narrator": ["win", "ctrl", "enter"],
  99.     "quick_language_list": ["win", "space"],
  100.     "open_system_control_panel": ["win", "pause"],
  101.     "start_task_manager": ["ctrl", "shift", "esc"],
  102.     "start_on_screen_keyboard": ["ctrl", "win", "o"],
  103.     "open_office_application_w": ["ctrl", "shift", "alt", "win", "w"],
  104.     "open_office_application_p": ["ctrl", "shift", "alt", "win", "p"],
  105.     "open_office_application_x": ["ctrl", "shift", "alt", "win", "x"],
  106.     "open_office_application_o": ["ctrl", "shift", "alt", "win", "o"],
  107.     "open_office_application_t": ["ctrl", "shift", "alt", "win", "t"],
  108.     "open_office_application_d": ["ctrl", "shift", "alt", "win", "d"],
  109.     "open_office_application_n": ["ctrl", "shift", "alt", "win", "n"],
  110.     "open_office_application_l": ["ctrl", "shift", "alt", "win", "l"],
  111.     "open_office_application_y": ["ctrl", "shift", "alt", "win", "y"],
  112. }
  113.  
  114. def select_program():
  115.     """
  116.    Opens the Program Files directory in File Explorer, allowing users to select a program.
  117.  
  118.    This function is used to assist users in locating and selecting the executable file for a program they wish to assign a custom hotkey to.
  119.    """
  120.     os.system("explorer C:\\Program Files (x86)")
  121.  
  122. def load_hotkeys():
  123.     """
  124.    Loads predefined and custom hotkeys from a JSON file.
  125.  
  126.    Returns:
  127.        dict: A dictionary containing both predefined and custom hotkeys. Initializes with predefined hotkeys if the file does not exist.
  128.    """
  129.     try:
  130.         with open("hotkeys.json", "r") as file:
  131.             hotkeys = json.load(file)
  132.     except FileNotFoundError:
  133.         hotkeys = {"known": PREDEFINED_HOTKEYS, "custom": {}}
  134.     return hotkeys
  135.  
  136. def save_hotkeys(hotkeys):
  137.     """
  138.    Saves the current hotkeys configuration to a JSON file.
  139.  
  140.    Args:
  141.        hotkeys (dict): A dictionary containing both predefined and custom hotkeys to be saved.
  142.    """
  143.     with open("hotkeys.json", "w") as file:
  144.         json.dump(hotkeys, file, indent=4)
  145.  
  146. def get_hotkey_input():
  147.     """
  148.    Captures a custom hotkey sequence from the user. The sequence ends when the 'Esc' key is pressed.
  149.  
  150.    Returns:
  151.        list: A list of strings representing the custom hotkey sequence entered by the user.
  152.    """
  153.     print("Press your custom hotkey sequence. Press 'Esc' to finish input.")
  154.     hotkey = []
  155.     modifiers = set()  # Track pressed modifier keys
  156.     while True:
  157.         key = keyboard.read_event(suppress=True)
  158.         if key.event_type == "down":
  159.             if key.name == "esc":
  160.                 break
  161.             elif key.name in ["shift", "ctrl", "alt", "win"]:
  162.                 modifiers.add(key.name)
  163.             else:
  164.                 hotkey.append("+".join(modifiers))
  165.                 hotkey.append(key.name)
  166.                 modifiers.clear()
  167.                 print("Current input:", "+".join(hotkey))
  168.     return hotkey
  169.  
  170. def create_custom_hotkey():
  171.     """
  172.    Allows users to create a custom hotkey by selecting a program and assigning a hotkey sequence to it. Saves the custom hotkey to the JSON file.
  173.  
  174.    This function guides the user through the process of defining a new hotkey sequence and associating it with a specific program.
  175.    """
  176.     hotkeys = load_hotkeys()
  177.     hotkey = get_hotkey_input()
  178.     hotkey_str = "+".join(hotkey)
  179.     print("Your input:", hotkey_str)
  180.     if hotkey_str in hotkeys["known"]:
  181.         print(
  182.             f"The hotkey '{hotkey_str}' already exists for {hotkeys['known'][hotkey_str]}."
  183.         )
  184.         print(
  185.             "Do you want to overwrite it or input a different sequence? (1 to Overwrite, 0 to Input Different)"
  186.         )
  187.         choice = input().strip()
  188.         if choice == "0":
  189.             return
  190.     elif hotkey_str in hotkeys["custom"]:
  191.         print(
  192.             f"The hotkey '{hotkey_str}' already exists for {hotkeys['custom'][hotkey_str]}."
  193.         )
  194.         print(
  195.             "Do you want to overwrite it or input a different sequence? (1 to Overwrite, 0 to Input Different)"
  196.         )
  197.         choice = input().strip()
  198.         if choice == "0":
  199.             return
  200.     select_program()  # Open file explorer
  201.     program_path = input("Enter the program path or select the program file: ").strip()
  202.     hotkeys["custom"][hotkey_str] = program_path
  203.     save_hotkeys(hotkeys)
  204.  
  205. if __name__ == "__main__":
  206.     create_custom_hotkey()
  207.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement