Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- # -*- coding: utf-8 -*-
- # Filename: custom_hotkey_manager_cli.py
- # Version: 1.0.0
- # Author: Jeoi Reqi
- """
- :: Windows Custom Hotkey Manager CLI Tool ::
- Description:
- - This script is a command-line interface (CLI) tool designed for managing and creating custom hotkeys in Windows.
- - It allows users to define custom hotkey sequences for launching specific programs and provides a predefined set of useful hotkeys for various Windows actions.
- - By interacting with the script, users can easily create and manage custom hotkeys without needing to access system settings directly.
- - Additionally, the script saves custom hotkeys to a JSON file, ensuring they are persistent across sessions.
- Requirements:
- - 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.
- (SEE COMPATIBILITY BELOW FOR FURTHER DETAILS)
- - It requires Python 3.x and the `keyboard` library to be installed on the system.
- - The script requires administrative privileges to set some hotkeys.
- Usage:
- - Run the script in a terminal or command prompt.
- - Follow the on-screen instructions to create or manage hotkeys.
- Compatibility:
- - Windows 10 and Windows 11:
- These hotkeys are fully supported.
- - Windows 8 and Windows 8.1:
- Most of these hotkeys are supported, though some features (like virtual desktops) were not introduced until Windows 10.
- - Windows 7:
- Basic hotkeys like opening the start menu, maximizing windows, and taking screenshots are supported,
- but many of the newer features like virtual desktops and some accessibility functions are not present.
- Note:
- - This script will work on Windows 7 and up, but some features may not be available in older versions.
- If a feature is not present, the script will handle it gracefully by returning to the menu without errors.
- Functions:
- 1. select_program():
- - Opens the Program Files directory in File Explorer, allowing users to select a program.
- 2. load_hotkeys():
- - Loads predefined and custom hotkeys from a JSON file. If the file does not exist, it initializes with predefined hotkeys.
- 3. save_hotkeys(hotkeys):
- - Saves the current hotkeys configuration to a JSON file.
- 4. get_hotkey_input():
- - Captures a custom hotkey sequence from the user. The sequence ends when the 'Esc' key is pressed.
- 5. create_custom_hotkey():
- - 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.
- Additional Notes:
- - Ensure that the script is run with appropriate permissions, especially when setting system-wide hotkeys.
- - Users should exercise caution when assigning hotkeys to avoid conflicts with existing system hotkeys.
- """
- import json
- import keyboard
- import os
- # Predefined hotkeys
- PREDEFINED_HOTKEYS = {
- "open_start_menu": ["win"],
- "open_secret_start_menu": ["win", "x"],
- "cycle_through_taskbar": ["win", "t"],
- "go_to_nth_application": ["win", "[n]"],
- "show_all_running_applications": ["win", "tab"],
- "show_hide_desktop": ["win", "d"],
- "minimize_all_windows": ["ctrl", "m"],
- "temporary_show_desktop": ["win", ","],
- "magnify_screen_content": ["win", "plus"],
- "maximize_window": ["win", "up"],
- "maximize_window_vertically": ["win", "shift", "up"],
- "move_window_to_left_monitor": ["custom"],
- "move_window_to_right_monitor": ["custom"],
- "take_rectangular_screenshot": ["win", "shift", "s"],
- "take_full_screenshot": ["win", "printscreen"],
- "create_new_virtual_desktop": ["win", "ctrl", "d"],
- "move_between_virtual_desktops_left": ["win", "ctrl", "left"],
- "move_between_virtual_desktops_right": ["win", "ctrl", "right"],
- "close_current_virtual_desktop": ["win", "ctrl", "f4"],
- "open_action_center": ["win", "a"],
- "open_search": ["win", "s"],
- "open_new_edge_tab": ["win", "c"],
- "open_windows_settings": ["win", "i"],
- "connect_sidebar": ["win", "k"],
- "use_voice_typing": ["win", "h"],
- "lock_computer": ["win", "l"],
- "lock_screen_orientation": ["win", "o"],
- "open_presentation_sidebar": ["win", "p"],
- "open_ease_of_access_center": ["win", "u"],
- "select_from_clipboard_history": ["win", "v"],
- "set_focus_to_notification_area": ["win", "b"],
- "open_emoji_panel": ["win", "."],
- "start_stop_narrator": ["win", "ctrl", "enter"],
- "quick_language_list": ["win", "space"],
- "open_system_control_panel": ["win", "pause"],
- "start_task_manager": ["ctrl", "shift", "esc"],
- "start_on_screen_keyboard": ["ctrl", "win", "o"],
- "open_office_application_w": ["ctrl", "shift", "alt", "win", "w"],
- "open_office_application_p": ["ctrl", "shift", "alt", "win", "p"],
- "open_office_application_x": ["ctrl", "shift", "alt", "win", "x"],
- "open_office_application_o": ["ctrl", "shift", "alt", "win", "o"],
- "open_office_application_t": ["ctrl", "shift", "alt", "win", "t"],
- "open_office_application_d": ["ctrl", "shift", "alt", "win", "d"],
- "open_office_application_n": ["ctrl", "shift", "alt", "win", "n"],
- "open_office_application_l": ["ctrl", "shift", "alt", "win", "l"],
- "open_office_application_y": ["ctrl", "shift", "alt", "win", "y"],
- }
- def select_program():
- """
- Opens the Program Files directory in File Explorer, allowing users to select a program.
- 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.
- """
- os.system("explorer C:\\Program Files (x86)")
- def load_hotkeys():
- """
- Loads predefined and custom hotkeys from a JSON file.
- Returns:
- dict: A dictionary containing both predefined and custom hotkeys. Initializes with predefined hotkeys if the file does not exist.
- """
- try:
- with open("hotkeys.json", "r") as file:
- hotkeys = json.load(file)
- except FileNotFoundError:
- hotkeys = {"known": PREDEFINED_HOTKEYS, "custom": {}}
- return hotkeys
- def save_hotkeys(hotkeys):
- """
- Saves the current hotkeys configuration to a JSON file.
- Args:
- hotkeys (dict): A dictionary containing both predefined and custom hotkeys to be saved.
- """
- with open("hotkeys.json", "w") as file:
- json.dump(hotkeys, file, indent=4)
- def get_hotkey_input():
- """
- Captures a custom hotkey sequence from the user. The sequence ends when the 'Esc' key is pressed.
- Returns:
- list: A list of strings representing the custom hotkey sequence entered by the user.
- """
- print("Press your custom hotkey sequence. Press 'Esc' to finish input.")
- hotkey = []
- modifiers = set() # Track pressed modifier keys
- while True:
- key = keyboard.read_event(suppress=True)
- if key.event_type == "down":
- if key.name == "esc":
- break
- elif key.name in ["shift", "ctrl", "alt", "win"]:
- modifiers.add(key.name)
- else:
- hotkey.append("+".join(modifiers))
- hotkey.append(key.name)
- modifiers.clear()
- print("Current input:", "+".join(hotkey))
- return hotkey
- def create_custom_hotkey():
- """
- 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.
- This function guides the user through the process of defining a new hotkey sequence and associating it with a specific program.
- """
- hotkeys = load_hotkeys()
- hotkey = get_hotkey_input()
- hotkey_str = "+".join(hotkey)
- print("Your input:", hotkey_str)
- if hotkey_str in hotkeys["known"]:
- print(
- f"The hotkey '{hotkey_str}' already exists for {hotkeys['known'][hotkey_str]}."
- )
- print(
- "Do you want to overwrite it or input a different sequence? (1 to Overwrite, 0 to Input Different)"
- )
- choice = input().strip()
- if choice == "0":
- return
- elif hotkey_str in hotkeys["custom"]:
- print(
- f"The hotkey '{hotkey_str}' already exists for {hotkeys['custom'][hotkey_str]}."
- )
- print(
- "Do you want to overwrite it or input a different sequence? (1 to Overwrite, 0 to Input Different)"
- )
- choice = input().strip()
- if choice == "0":
- return
- select_program() # Open file explorer
- program_path = input("Enter the program path or select the program file: ").strip()
- hotkeys["custom"][hotkey_str] = program_path
- save_hotkeys(hotkeys)
- if __name__ == "__main__":
- create_custom_hotkey()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement