Swiftpack.co - Kyome22/SpiceKey as Swift Package

Swiftpack.co is a collection of thousands of indexed Swift packages. Search packages.
See all packages published by Kyome22.
Kyome22/SpiceKey 4.5.0
Global Shortcuts for macOS written in Swift.
⭐️ 12
🕓 3 weeks ago
macOS
.package(url: "https://github.com/Kyome22/SpiceKey.git", from: "4.5.0")

SpiceKey

Global Shortcuts for macOS written in Swift.

Demo

Demo App is in this Project.

demo_appkit demo_swiftui

Usage

  • Register a shortcut

Set ⌘ + A shortcut.

let key = Key.a
let modifierFlags = ModifierFlags.cmd
let keyCombo = KeyCombination(key, modifierFlags)
let spiceKey = SpiceKey(keyCombo, keyDownHandler: {
    // process (key down)
}, keyUpHandler: {
    // process (key up)
})
spiceKey.register()

Set press both side of ⌘ shortcut.

let flag = ModifierFlag.command
let spiceKey = SpiceKey(flag, bothModifierKeysPressHandler: {
    // process (press)
}, releaseKeyHandler: {
    // process (release)
})
spiceKey.register()

Set long press ⌘ shortcut.

// run after 0.6 sec
let flags = ModifierFlags.cmd
let spiceKey = SpiceKey(flags, 0.6, modifierKeysLongPressHandler: {
    // process (press)
}, releaseKeyHandler: {
    // process (release)
})
spiceKey.register()
  • Create a Key and a ModifierFlags from NSEvent.
func example(event: NSEvent) {
    let key = Key(keyCode: event.keyCode)
    let flags = event.modifierFlags.intersection(.deviceIndependentFlagsMask)
    let modifierFlags = ModifierFlags(flags: flags)
}
  • Get a description of the shortcut
let description = modifierFlags.string + key.string
// or
let description = keyCombination.string
  • Unregister a shortcut
spiceKey.unregister()
  • Save shortcut

SpiceKeyData supports NSCoding & Codable.

let key = Key.a
let flags = ModifierFlags.optCmd
let spiceKey = SpiceKey(...)

let spiceKeyData = SpiceKeyData(_ primaryKey: String,
                                _ keyCode: CGKeyCode,  // key.keyCode
                                _ control: Bool,       // flags.containsControl
                                _ option: Bool,        // flags.containsOption
                                _ shift: Bool,         // flags.containsShift
                                _ command: Bool,       // flags.containsCommand
                                _ spiceKey: SpiceKey)  // spiceKey
// OR
let spiceKeyData = SpiceKeyData(_ primaryKey: String,
                                _ key: Key,
                                _ modifierFlags: ModifierFlags,
                                _ spiceKey: SpiceKey)

let data = try! NSKeyedArchiver.archivedData(withRootObject: spiceKeyData,
                                             requiringSecureCoding: false)
UserDefaults.standard.set(data, forKey: "spiceKeyData")
  • Load shortcut
let data = UserDefaults.standard.data(forKey: "spiceKeyData")!
let spiceKeyData = try! NSKeyedUnarchiver
    .unarchiveTopLevelObjectWithData(data) as! SpiceKeyData

SpiceKeyField & SKTextField

A special text field that can be used to register SpiceKey.

  • SpiceKeyField is for AppKit only.
  • SKTextField is a SpiceKeyField made available from SwiftUI.

Please see the Demo App for detailed instructions.

GitHub

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

Release Notes

4.4.0
4 weeks ago

SpiceKeyData supports Codable

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