<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftUI",
  "identifier" : "/documentation/SwiftUI/View/onKeyPress(keys:phases:action:)",
  "metadataVersion" : "0.1.0",
  "role" : "Instance Method",
  "symbol" : {
    "kind" : "Instance Method",
    "modules" : [
      "SwiftUI"
    ],
    "preciseIdentifier" : "s:7SwiftUI4ViewPAAE10onKeyPress4keys6phases6actionQrShyAA0E10EquivalentVG_AA0eF0V6PhasesVAL6ResultOALctF"
  },
  "title" : "onKeyPress(keys:phases:action:)"
}
-->

# onKeyPress(keys:phases:action:)

Performs an action if the user presses one or more keys on a hardware
keyboard while the view has focus.

```
nonisolated func onKeyPress(keys: Set<KeyEquivalent>, phases: KeyPress.Phases = [.down, .repeat], action: @escaping (KeyPress) -> KeyPress.Result) -> some View
```

## Parameters

`keys`

A set of keys to match against incoming hardware
keyboard events.

`phases`

The key-press phases to match (`.down`, `.repeat`, and
`.up`). The default value is `[.down, .repeat]`.

`action`

The action to perform. The action receives a value
describing the matched key event. Return `.handled` to consume the
event and prevent further dispatch, or `.ignored` to allow dispatch
to continue.

## Return Value

A modified view that binds keyboard input when focused.

---

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)