---
title: "keyboardShortcut(_:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/scene/keyboardshortcut(_:)"
---

# keyboardShortcut(_:)

Defines a keyboard shortcut for opening new scene windows.

## Declaration

```swift
nonisolated func keyboardShortcut(_ shortcut: KeyboardShortcut?) -> some Scene

```

## Parameters

- `shortcut`: The keyboard shortcut for presenting the scene, or nil.

## Mentioned in

Building and customizing the menu bar with SwiftUI

## Return Value

Return Value A scene that can be presented with a keyboard shortcut.

## Discussion

Discussion A scene’s keyboard shortcut is bound to the command it adds for creating new windows (in the case of WindowGroup and DocumentGroup) or bringing a singleton window forward (in the case of Window and, on macOS, Settings and UtilityWindow). Pressing the keyboard shortcut is equivalent to selecting the menu command. In cases where a command already has a keyboard shortcut, the scene’s keyboard shortcut is used instead. For example, WindowGroup normally creates a File > New Window menu command whose keyboard shortcut is ⌘N. The following code changes it to something based on dynamic state: @main struct Notes: App {     @State private var newWindowShortcut: KeyboardShortcut? = ...

var body: some Scene {         WindowGroup {             ContentView($newWindowShortcut)         }         .keyboardShortcut(newWindowShortcut)     } } If shortcut is nil, the scene’s presentation command will not be associated with a keyboard shortcut, even if SwiftUI normally assigns one automatically.

## See Also

### Setting commands

- [commands(content:)](swiftui/scene/commands(content:).md)
- [commandsRemoved()](swiftui/scene/commandsremoved().md)
- [commandsReplaced(content:)](swiftui/scene/commandsreplaced(content:).md)
- [keyboardShortcut(_:modifiers:localization:)](swiftui/scene/keyboardshortcut(_:modifiers:localization:).md)
