Created
October 28, 2025 10:09
-
-
Save hasansezertasan/b130d3795eeb19dda83cbbba3050519a to your computer and use it in GitHub Desktop.
Hotkey registration module.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # /// script | |
| # requires-python = ">=3.13" | |
| # dependencies = [ | |
| # "pynput", | |
| # ] | |
| # /// | |
| # ruff: noqa: T201 | |
| """Hotkey registration module.""" | |
| from collections.abc import Callable | |
| from functools import wraps | |
| from pynput import keyboard | |
| hotkey_registry: dict[str, Callable[[], None]] = {} | |
| """Registry of hotkeys and their corresponding functions.""" | |
| def hotkey(name: str) -> Callable[[Callable[[], None]], Callable[[], None]]: | |
| """Register a hotkey and its corresponding function.""" | |
| def decorator(func: Callable[[], None]) -> Callable[[], None]: | |
| """Decorator to register a hotkey and its corresponding function.""" | |
| @wraps(func) | |
| def wrapper() -> None: | |
| """Wrapper to register a hotkey and its corresponding function.""" | |
| return func() | |
| hotkey_registry[name] = wrapper | |
| return wrapper | |
| return decorator | |
| @hotkey("<ctrl>+<alt>+h") | |
| def _() -> None: | |
| print("<ctrl>+<alt>+h pressed") | |
| @hotkey("<ctrl>+<alt>+o") | |
| def _() -> None: | |
| print("<ctrl>+<alt>+o pressed") | |
| def main() -> None: | |
| listener = keyboard.GlobalHotKeys(hotkey_registry) | |
| listener.start() | |
| while True: | |
| pass | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment