Enumeration
KeyboardShortcuts
public enum KeyboardShortcuts
Global keyboard shortcuts for your macOS app.
Relationships
Nested Types
KeyboardShortcuts.Key
Represents a key on the keyboard.
KeyboardShortcuts.Name
The strongly-typed name of the keyboard shortcut.
KeyboardShortcuts.Recorder
A SwiftUI
View
that lets the user record a keyboard shortcut.KeyboardShortcuts.RecorderCocoa
A
NSView
that lets the user record a keyboard shortcut.KeyboardShortcuts.Shortcut
A keyboard shortcut.
Nested Type Aliases
KeyAction
public typealias KeyAction = () -> Void
:nodoc:
Properties
registeredShortcuts
var registeredShortcuts
keyDownHandlers
var keyDownHandlers
keyUpHandlers
var keyUpHandlers
userDefaultsKeyDownHandlers
var userDefaultsKeyDownHandlers
userDefaultsKeyUpHandlers
var userDefaultsKeyUpHandlers
userDefaultsPrefix
let userDefaultsPrefix
Methods
register(_:)
private static func register(_ shortcut: Shortcut)
unregister(_:)
private static func unregister(_ shortcut: Shortcut)
unregisterAll()
static func unregisterAll()
disable(_:)
public static func disable(_ name: Name)
Disable a keyboard shortcut.
enable(_:)
public static func enable(_ name: Name)
Enable a disabled keyboard shortcut.
handleOnKeyDown(_:)
private static func handleOnKeyDown(_ shortcut: Shortcut)
handleOnKeyUp(_:)
private static func handleOnKeyUp(_ shortcut: Shortcut)
onKeyDown(for:action:)
public static func onKeyDown(for name: Name, action: @escaping KeyAction)
Listen to the keyboard shortcut with the given name being pressed.
You can register multiple listeners.
You can safely call this even if the user has not yet set a keyboard shortcut. It will just be inactive until they do.
import Cocoa
import KeyboardShortcuts
@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
KeyboardShortcuts.onKeyDown(for: .toggleUnicornMode) {
self.isUnicornMode.toggle()
}
}
}
onKeyUp(for:action:)
public static func onKeyUp(for name: Name, action: @escaping KeyAction)
Listen to the keyboard shortcut with the given name being pressed.
You can register multiple listeners.
You can safely call this even if the user has not yet set a keyboard shortcut. It will just be inactive until they do.
import Cocoa
import KeyboardShortcuts
@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
func applicationDidFinishLaunching(_ notification: Notification) {
KeyboardShortcuts.onKeyUp(for: .toggleUnicornMode) {
self.isUnicornMode.toggle()
}
}
}
userDefaultsKey(for:)
private static func userDefaultsKey(for shortcutName: Name) -> String
userDefaultsDidChange(name:)
static func userDefaultsDidChange(name: Name)
userDefaultsGet(name:)
static func userDefaultsGet(name: Name) -> Shortcut?
userDefaultsSet(name:shortcut:)
static func userDefaultsSet(name: Name, shortcut: Shortcut)
userDefaultsRemove(name:)
static func userDefaultsRemove(name: Name)