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.