Swiftpack.co - jordanbaird/SwiftKeys as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by jordanbaird.
jordanbaird/SwiftKeys 0.0.5
A Swifty API for global macOS hotkeys.
⭐️ 5
🕓 1 week ago
.package(url: "https://github.com/jordanbaird/SwiftKeys.git", from: "0.0.5")

SwiftKeys

Continuous Integration Code Coverage Release Swift License

A Swifty API for global macOS hotkeys.

Install

Add the following dependency to your Package.swift file:

.package(url: "https://github.com/jordanbaird/SwiftKeys", from: "0.0.5")

Usage

Read the full documentation here

Start by creating an instance of KeyEvent. Then, use it to initialize a KeyRecorder.

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

The recorder and the event will stay synchronized with each other, so when the user records a new key combination, the event will update to the new value. You can also observe the event and perform actions on both key-up and key-down.

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

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 name "ShowPreferences" would become "SKShowPreferences" when used as a defaults key.

The following pseudo-code is what a typical view controller that utilizes SwiftKeys might look like:

import SwiftKeys

class ViewController: NSViewController {
    let event = KeyEvent(name: "SomeEvent")
    let recorder = KeyRecorder(keyEvent: event)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubview(recorder)
        
        event.observe(.keyUp) {
            print("UP")
        }
        event.observe(.keyDown) {
            print("DOWN")
        }
    }
}

License

SwiftKeys is licensed under the MIT license.

GitHub

link
Stars: 5
Last commit: 2 days ago
jonrohan Something's broken? Yell at me @ptrpavlik. Praise and feedback (and money) is also welcome.

Release Notes

SwiftKeys 0.0.5
1 week ago

Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics