HotKey: Global shortcut for macOS

HotKey is a free open-source simple global shortcuts in macOS. HotKey wraps the Carbon APIs for dealing with global hot keys to make it easy to use in Swift.

How to install?

It can be installed using several methods such as

  • Swift Package Manager
  • Cartage
  • CocoaPods

Swift Package Manager

For installation with Swift Package Manager, simply add the following to your Package.swift:

.package(url: "https://github.com/soffes/HotKey", from: "0.2.0")

Carthage

For installation with Carthage, simply add the following to your Cartfile:

github "soffes/HotKey"

CocoaPods

For installation with CocoaPods, simply add the following to your Podfile:

pod 'HotKey'

How to use?

Usage

Simply initialize a HotKey with a key and modifiers:

// Setup hot key for ⌥⌘Rlet hotKey = HotKey(key: .r, modifiers: [.command, .option])

This is a convenience initializer that creates a KeyCombo for you. You can also initialize with a Carbon key code and Carbon modifier flags if you’re feeling old school.

Now you can set the keyDownHandler and get callbacks for when your hot key is pressed:

hotKey.keyDownHandler = { print("Pressed at \(Date())") }

You can also implement hotKey.keyUpHandler if you’d like.

You don’t need to think about when handlers are registered and unregistered. This all happens automatically based on the HotKey object’s lifecycle.


Tech

  • Swift and Swift UI

License

  • MIT license

Resources