SwiftKeys

Continuous Integration Release Swift License

A Swifty API for global macOS hotkeys.

Install

Add the following to your Package.swift file:

import PackageDescription

let package = Package(
    name: "PackageName",
    dependencies: [
        .package(url: "https://github.com/jordanbaird/SwiftKeys", from: "0.0.2")
    ],
    targets: [
        .target(
            name: "PackageName",
            dependencies: ["SwiftKeys"]
        )
    ]
)

Usage

Start by creating an instance of KeyEvent. Then, use it to initialize a KeyRecorder instance. The recorder will stay synchronized with the key event, so that when it records a new key combination the key event will update in accordance to the new value. You can also observe the event and perform actions on both key-down and key-up.

let event = KeyEvent(name: "SomeEvent")
let recorder = KeyRecorder(keyEvent: event)

event.observe(.keyDown) {
    print("DOWN")
}
event.observe(.keyUp) {
    print("UP")
}

For improved type safety, you can create hard-coded key event names that can be referenced across your app.

extension KeyEvent.Name {
    static let showPreferences = Self("ShowPreferences")
}
let event = KeyEvent(name: .showPreferences)

Key events are automatically stored in the UserDefaults system, using their names as keys. You can provide a custom prefix that will be combined with each name to create the keys.

extension KeyEvent.Name.Prefix {
    public override var sharedPrefix: Self { 
        Self("SK")
    }
}

The showPreferences name from above would become “SKShowPreferences” when used as a UserDefaults key.

Read full documentation here

License

SwiftKeys is licensed under the MIT license. See the LICENSE file for details.

GitHub

View Github