NEW: Start my new Ultimate Portfolio App course with a free Hacking with Swift+ trial! >>

How to add keyboard shortcuts using keyboardShortcut()

Paul Hudson    @twostraws   

Updated for Xcode 12.5

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")

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 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])
}

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)
Hacking with Swift is sponsored by Emerge

SPONSORED Emerge helps iOS devs write better, smaller apps by profiling binary size on each pull request and surfacing insights and suggestions. Companies using Emerge have reduced the size of their apps by up to 50% in just the first day. Built by a team with years of experience reducing app size at Airbnb.

Set up a demo!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Similar solutions…

BUY OUR BOOKS
Buy Pro Swift Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift (Vapor Edition) Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Server-Side Swift (Kitura Edition) Buy Beyond Code

Was this page useful? Let us know!

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.