This package lets you add support for user-customizable global keyboard shortcuts to your macOS app in minutes. It’s fully sandbox and Mac App Store compatible. And it’s used in production by Dato, Jiffy, Plash, and Lungo.
I’m happy to accept more configurability and features. PR welcome! What you see here is just what I needed for my own apps.
KeyboardShortcuts.Recorder takes care of storing the keyboard shortcut in UserDefaults and also warning the user if the chosen keyboard shortcut is already used by the system or the app’s main menu.
Add a listener for when the user presses their chosen keyboard shortcut.
App.swift
import SwiftUI
import KeyboardShortcuts
@main
struct YourApp: App {
@StateObject private var appState = AppState()
var body: some Scene {
WindowGroup {
// …
}
Settings {
SettingsScreen()
}
}
}
@MainActor
final class AppState: ObservableObject {
init() {
KeyboardShortcuts.onKeyUp(for: .toggleUnicornMode) { [self] in
isUnicornMode.toggle()
}
}
}
You can also listen to key down with .onKeyDown()
That’s all! ✨
You can find a complete example in the “Example” directory.
import Cocoa
import KeyboardShortcuts
final class SettingsViewController: NSViewController {
override func loadView() {
view = NSView()
let recorder = KeyboardShortcuts.RecorderCocoa(for: .toggleUnicornMode)
view.addSubview(recorder)
}
}
Localization
This package supports localizations. PR welcome for more!
Fork the repo.
Create a directory that has a name that uses an ISO 639-1 language code and optional designators, followed by the .lproj suffix. More here.
Create a file named Localizable.strings under the new language directory and then copy the contents of KeyboardShortcuts/Localization/en.lproj/Localizable.strings to the new file that you just created.
Localize and make sure to review your localization multiple times. Check for typos.
Try to find someone that speaks your language to review the translation.
Your app might need to support keyboard shortcuts for user-defined actions. Normally, you would statically register the keyboard shortcuts upfront in extension KeyboardShortcuts.Name {}. However, this is not a requirement. It’s only for convenience so that you can use dot-syntax when calling various APIs (for example, .onKeyDown(.unicornMode) {}). You can create KeyboardShortcut.Name‘s dynamically and store them yourself. You can see this in action in the example project.
Default keyboard shortcuts
Setting a default keyboard shortcut can be useful if you’re migrating from a different package or just making something for yourself. However, please do not set this for a publicly distributed app. Users find it annoying when random apps steal their existing keyboard shortcuts. It’s generally better to show a welcome screen on the first app launch that lets the user set the shortcut.
HotKey is good for adding hard-coded keyboard shortcuts, but it doesn’t provide any UI component for the user to choose their own keyboard shortcuts.
Why is this package importing Carbon? Isn’t that deprecated?
Most of the Carbon APIs were deprecated years ago, but there are some left that Apple never shipped modern replacements for. This includes registering global keyboard shortcuts. However, you should not need to worry about this. Apple will for sure ship new APIs before deprecating the Carbon APIs used here.
Does this package cause any permission dialogs?
No.
How can I add an app-specific keyboard shortcut that is only active when the app is?
This package lets you add support for user-customizable global keyboard shortcuts to your macOS app in minutes. It’s fully sandbox and Mac App Store compatible. And it’s used in production by Dato, Jiffy, Plash, and Lungo.
I’m happy to accept more configurability and features. PR welcome! What you see here is just what I needed for my own apps.
Requirements
macOS 10.13+
Install
Add
https://github.com/sindresorhus/KeyboardShortcuts
in the “Swift Package Manager” tab in Xcode.Usage
First, register a name for the keyboard shortcut.
Constants.swift
You can then refer to this strongly-typed name in other places.
You will want to make a view where the user can choose a keyboard shortcut.
SettingsScreen.swift
There’s also support for Cocoa instead of SwiftUI.
KeyboardShortcuts.Recorder
takes care of storing the keyboard shortcut inUserDefaults
and also warning the user if the chosen keyboard shortcut is already used by the system or the app’s main menu.Add a listener for when the user presses their chosen keyboard shortcut.
App.swift
You can also listen to key down with
.onKeyDown()
That’s all! ✨
You can find a complete example in the “Example” directory.
You can also find a real-world example in my Plash app.
Cocoa
Using
KeyboardShortcuts.RecorderCocoa
instead ofKeyboardShortcuts.Recorder
:Localization
This package supports localizations. PR welcome for more!
.lproj
suffix. More here.Localizable.strings
under the new language directory and then copy the contents ofKeyboardShortcuts/Localization/en.lproj/Localizable.strings
to the new file that you just created.API
See the API docs.
Tips
Show a recorded keyboard shortcut in an
NSMenuItem
See
NSMenuItem#setShortcut
.Dynamic keyboard shortcuts
Your app might need to support keyboard shortcuts for user-defined actions. Normally, you would statically register the keyboard shortcuts upfront in
extension KeyboardShortcuts.Name {}
. However, this is not a requirement. It’s only for convenience so that you can use dot-syntax when calling various APIs (for example,.onKeyDown(.unicornMode) {}
). You can createKeyboardShortcut.Name
‘s dynamically and store them yourself. You can see this in action in the example project.Default keyboard shortcuts
Setting a default keyboard shortcut can be useful if you’re migrating from a different package or just making something for yourself. However, please do not set this for a publicly distributed app. Users find it annoying when random apps steal their existing keyboard shortcuts. It’s generally better to show a welcome screen on the first app launch that lets the user set the shortcut.
Get all keyboard shortcuts
To get all the keyboard shortcut
Name
‘s, conformKeyboardShortcuts.Name
toCaseIterable
.And to get all the
Name
‘s with a set keyboard shortcut:FAQ
How is it different from
MASShortcut
?This package:
NSMenuItem
.NSMenu
is open (e.g. menu bar apps).MASShortcut
:How is it different from
HotKey
?HotKey
is good for adding hard-coded keyboard shortcuts, but it doesn’t provide any UI component for the user to choose their own keyboard shortcuts.Why is this package importing
Carbon
? Isn’t that deprecated?Most of the Carbon APIs were deprecated years ago, but there are some left that Apple never shipped modern replacements for. This includes registering global keyboard shortcuts. However, you should not need to worry about this. Apple will for sure ship new APIs before deprecating the Carbon APIs used here.
Does this package cause any permission dialogs?
No.
How can I add an app-specific keyboard shortcut that is only active when the app is?
That is outside the scope of this package. You can either use
NSEvent.addLocalMonitorForEvents
,NSMenuItem
with keyboard shortcut (it can even be hidden), or SwiftUI’sView#keyboardShortcut()
modifier.Does it support media keys?
No, since it would not work for sandboxed apps. If your app is not sandboxed, you can use
MediaKeyTap
.Can you support CocoaPods or Carthage?
No. However, there is nothing stopping you from using Swift Package Manager for just this package even if you normally use CocoaPods or Carthage.
Related