Skip to content

ExtraKeys/jordanbaird-SwiftKeys

 
 

Repository files navigation

A Swifty API for global macOS hotkeys.


Continuous Integration Code Coverage Release Swift Version License

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")
    }
}

In the example above, 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.

About

A Swifty API for global macOS hotkeys.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Swift 98.8%
  • Shell 1.2%