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

# keyboardShortcut(_:modifiers:)

Defines a keyboard shortcut and assigns it to the modified control.

```
nonisolated func keyboardShortcut(_ key: KeyEquivalent, modifiers: EventModifiers = .command) -> 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,
depth-first 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.

The default localization configuration is set to [`automatic`](/documentation/SwiftUI/KeyboardShortcut/Localization-swift.struct/automatic).

---

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)