KeyboardShortcuts Documentation Beta

Enumeration Keyboard​Shortcuts

public enum KeyboardShortcuts

Global keyboard shortcuts for your macOS app.

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

Key​Action

public typealias KeyAction = () -> Void

:nodoc:

Properties

registered​Shortcuts

var registeredShortcuts

key​Down​Handlers

var keyDownHandlers

key​UpHandlers

var keyUpHandlers

user​Defaults​Key​Down​Handlers

var userDefaultsKeyDownHandlers

user​Defaults​Key​UpHandlers

var userDefaultsKeyUpHandlers

user​Defaults​Prefix

let userDefaultsPrefix

Methods

register(_:​)

private static func register(_ shortcut: Shortcut)

unregister(_:​)

private static func unregister(_ shortcut: Shortcut)

unregister​All()

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.

handle​OnKey​Down(_:​)

private static func handleOnKeyDown(_ shortcut: Shortcut)

handle​OnKey​Up(_:​)

private static func handleOnKeyUp(_ shortcut: Shortcut)

on​Key​Down(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()
		}
	}
}

on​Key​Up(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()
		}
	}
}

user​Defaults​Key(for:​)

private static func userDefaultsKey(for shortcutName: Name) -> String

user​Defaults​Did​Change(name:​)

static func userDefaultsDidChange(name: Name)

user​Defaults​Get(name:​)

static func userDefaultsGet(name: Name) -> Shortcut?

user​Defaults​Set(name:​shortcut:​)

static func userDefaultsSet(name: Name, shortcut: Shortcut)

user​Defaults​Remove(name:​)

static func userDefaultsRemove(name: Name)