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.2.0
Global macOS key commands.
⭐️ 14
🕓 6 weeks ago
.package(url: "https://github.com/jordanbaird/SwiftKeys.git", from: "0.2.0")

Global macOS key commands

Continuous Integration Release Swift Versions Docs License


Add the following dependency to your Package.swift file:

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


Read the full documentation here

Creating and Observing

Start by creating an instance of KeyCommand. Observe it, and perform actions on keyDown, keyUp, and doubleTap(_:):

let command = KeyCommand(name: "ToggleMainWindow")

command.observe(.keyDown) {

command.observe(.keyUp) {

command.observe(.doubleTap(0.2)) {

doubleTap(_:) allows you to specify a maximum time interval that the two key presses must fall within to be considered a "double-tap".

Adding a Key Recorder

Use the key command's name to create a key recorder. Then, add it to a view (note the use of KeyRecorderView for SwiftUI and KeyRecorder for Cocoa):


struct SettingsView: View {
    var body: some View {
        KeyRecorderView(name: "ToggleMainWindow")


class SettingsViewController: NSViewController {
    let recorder = KeyRecorder(name: "ToggleMainWindow")

    override func viewDidLoad() {

The result should look something like this:

Light mode Dark mode

The recorder and command will stay synchronized with each other, so when the user records a new key combination, the command will be updated to match the new value.

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


extension KeyCommand.Name {
    static let toggleMainWindow = Self("ToggleMainWindow")


let command = KeyCommand(name: .toggleMainWindow)


let recorder = KeyRecorder(name: .toggleMainWindow)

Key commands are automatically stored in the UserDefaults system, using their names as keys. It's common for UserDefaults keys to be prefixed, or namespaced, according to their corresponding app or subsystem. To that end, SwiftKeys lets you provide custom prefixes that can be applied to individual names.

extension KeyCommand.Name.Prefix {
    static let settings = Self("Settings")
    static let app = Self("MyGreatApp")

extension KeyCommand.Name {
    // "SettingsOpen" will be the full UserDefaults key.
    static let openSettings = Self("Open", prefix: .settings)
    // "MyGreatApp_Quit" will be the full UserDefaults key.
    static let quitApp = Self("Quit", prefix: .app, separator: "_")


SwiftKeys is available under the MIT license.


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

Release Notes

SwiftKeys 0.2.0
6 weeks ago



  • KeyCommand.isReservedBySystem(key:modifiers:) is now public. You can use it to determine if a key-modifier combination is available for use as a key command before actually creating the command.


  • typealias KeyCommand.Observation.EventType has been removed. Use KeyCommand.EventType instead.

Full Changelog: https://github.com/jordanbaird/SwiftKeys/compare/0.1.9...0.2.0

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