Skip to main content

How to add keyboard shortcuts using keyboardShortcut()

About 3 minSwiftSwiftUIArticle(s)bloghackingwithswift.comcrashcourseswiftswiftuixcodeappstore

How to add keyboard shortcuts using keyboardShortcut() 관련

SwiftUI by Example

Back to Home

How to add keyboard shortcuts using keyboardShortcut() | SwiftUI by Example

How to add keyboard shortcuts using keyboardShortcut()

Updated for Xcode 15

SwiftUI makes it easy to add keyboard shortcuts for devices that support it, such as iPadOS and macOS, all using the keyboardShortcut() modifier.

There are three ways you'll want to use this modifier, so let's start with the most basic: attaching a key to an existing action. For example, if we had a log in button and wanted to trigger its behavior when the user pressed Cmd+L we could do this:

Button("Log in") {
    print("Authenticating…")
}
.keyboardShortcut("l")

Download this as an Xcode projectopen in new window

Note that we don't need to specify that we mean Cmd+L, because SwiftUI assumes the Command key is used unless we specify otherwise. If you run that code sample on an iPad, you'll see that holding down the Cmd key brings up the keyboard shortcuts overlay, showing “Cmd+L Login” already – SwiftUI automatically figured out what our button did and made it available.

The words “Log in” in blue, indicating they are tappable. Below that is iPadOS's command palette showing the Log In command has the <kbd>Cmd</kbd>+<kbd>L</kbd> shortcut.
The words “Log in” in blue, indicating they are tappable. Below that is iPadOS's command palette showing the Log In command has the Cmd+L shortcut.

The second way to use keyboardShortcut() is to specify which modifier keys you actually want. As an example, this creates two more buttons, one using Shift+R to trigger a Run button, and another for Ctrl+Opt+Cmd+H to trigger a Home button:

VStack {
    Button("Run") {
        print("Running…")
    }
    .keyboardShortcut("r", modifiers: .shift)

    Button("Home") {
        print("Going home")
    }
    .keyboardShortcut("h", modifiers: [.control, .option, .command])
}

Download this as an Xcode projectopen in new window

The words “Run” and “Home” in blue, indicating they are tappable. Below that is iPadOS's command palette showing that “Run” has the shortcut <kbd>Shift</kbd>+<kbd>R</kbd>, and “Home” has shortcut <kbd>Ctrl</kbd>+<kbd>Opt</kbd>+<kbd>Cmd</kbd>+<kbd>H</kbd>.
The words “Run” and “Home” in blue, indicating they are tappable. Below that is iPadOS's command palette showing that “Run” has the shortcut Shift+R, and “Home” has shortcut Ctrl+Opt+Cmd+H.

That shows you how to select one custom modifier, and how to select several modifiers at the same time.

The third and final way to use keyboardShortcut() is with one of its built-in keys, which are useful for hard to type keys such as Escape and arrows, and also for semantic keys, such as a cancellation action and a default action. Semantic keys are really useful – every time you've pressed Return to accept the default action of an alert, or pressed Escape to cancel an action, you've used semantic keys.

So, this creates a button with a default action shortcut, meaning that pressing Return will trigger it:

Button("Confirm Launch") {
    print("Launching drone…")
}
.keyboardShortcut(.defaultAction)

Download this as an Xcode projectopen in new window

The words “Confirm Launch” in blue, indicating they are tappable. Below that is iPadOS's command palette showing the Confirm Launch command has the <kbd>Return</kbd> or <kbd>Enter</kbd> shortcut.
The words “Confirm Launch” in blue, indicating they are tappable. Below that is iPadOS's command palette showing the Confirm Launch command has the Return or Enter shortcut.
Similar solutions…
How to add a toolbar to the keyboard | SwiftUI by Example

How to add a toolbar to the keyboard
How to dismiss the keyboard for a TextField | SwiftUI by Example

How to dismiss the keyboard for a TextField
How to dismiss the keyboard when the user scrolls | SwiftUI by Example

How to dismiss the keyboard when the user scrolls
How to make buttons that repeat their action when pressed | SwiftUI by Example

How to make buttons that repeat their action when pressed
How to add advanced text styling using AttributedString | SwiftUI by Example

How to add advanced text styling using AttributedString

이찬희 (MarkiiimarK)
Never Stop Learning.