INNER CODE UNIT · Python
add_hotkey
spyoungtech/ahk · ahk/_async/engine.py:194
def add_hotkey(
self, keyname: str, callback: Callable[[], Any], ex_handler: Optional[Callable[[str, Exception], Any]] = None
) -> None:
"""
Register a function to be called when a hotkey is pressed.
Key notes:
- You must call the `start_hotkeys` method for the hotkeys to be active
- All hotkeys run in a single AHK process instance (but Python callbacks also get their own thread and can run simultaneously)
- If you add a hotkey after the hotkey thread/instance is active, it will be restarted automatically
- `async` functions are not directly supported as callbacks, however you may write a synchronous function that calls `asyncio.run`/`loop.create_task` etc.
:param keyname: the key trigger for the hotkey, such as ``#n`` (win+n)
:param callback: callback function to call when the hotkey is triggered
:param ex_handler: a function which accepts two parameters: the keyname for the hotkey and the exception raised by the callback function.
:return:
"""