Add Keyboard Shortcuts to iPad App

loading views

I wanted to add useful keyboard shortcuts for Pagi for iPad this morning and was afraid I would have to use some old UIKit-API to do this.

Luckily this is now pretty easy to achieve when building apps with the SwiftUI-lifecycle.

All you have to do is to use the .keyboardShortcut() modifier on a Button or some other compatible view and wrap it inside the .commands() modifier in your app's main file.

When a user long-presses CMD inside the app a menu will appear that will show all defined shortcuts inside the app.

Code

WindowGroup {
    ContentView()
}
.commands {
    CommandMenu("File") {
        Button("Save") {
            // some code
        }
        .keyboardShortcut("E", modifiers: .command)     
    }
}

Demo

iPad screenshot of shortcuts