Skip to main content

Bind a global hotkey, on macOS, in Python

Project description

This is a set of minimal Python bindings for the undocumented macOS framework APIs that even the most modern, sandboxing-friendly shortcut-binding frameworks use under the hood for actually binding global hotkeys.

Unlike something full-featured like MASShortcut, this provides no configuration UI; you have to provide keyboard constants directly.

Simple example:

from quickmachotkey import quickHotKey, mask
from quickmachotkey.constants import kVK_ANSI_X, cmdKey, controlKey, optionKey

@quickHotKey(virtualKey=kVK_ANSI_X, modifierMask=mask(cmdKey, controlKey, optionKey))
def handler() -> None:
    print("handled ⌘⌃⌥X")

if __name__ == "__main__":
    from AppKit import NSApplication  # type:ignore[import]
    from PyObjCTools import AppHelper  # type:ignore[import]
    print("type ⌘⌃⌥X with any application focused")
    NSApplication.sharedApplication()
    AppHelper.runEventLoop()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

quickmachotkey-2023.11.17.tar.gz (8.9 kB view hashes)

Uploaded Source

Built Distribution

quickmachotkey-2023.11.17-py3-none-any.whl (9.9 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page