<!--
{
  "availability" : [
    "iOS: 15.4.0 -",
    "iPadOS: 15.4.0 -",
    "macCatalyst: 15.4.0 -",
    "macOS: 12.3.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/keyboardShortcut(_:)-3vjx6",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE16keyboardShortcutyQrAA08KeyboardE0VSgF"
  },
  "title" : "keyboardShortcut(_:)"
}
-->

# keyboardShortcut(_:)

Assigns an optional keyboard shortcut to the modified control.

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

## Discussion

Pressing the control’s shortcut while the control is anywhere in the
frontmost window or scene, or anywhere in the macOS main menu, is
equivalent to direct interaction with the control to perform its primary
action.

The target of a keyboard shortcut is resolved in a leading-to-trailing
traversal of one or more view hierarchies. On macOS, the system looks in
the key window first, then the main window, and then the command groups;
on other platforms, the system looks in the active scene, and then the
command groups.

If multiple controls are associated with the same shortcut, the first
one found is used. If the provided shortcut is `nil`, the modifier will
have no effect.

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)