---
title: "onKeyPress(phases:action:)"
framework: swiftui
role: symbol
role_heading: Instance Method
path: "swiftui/view/onkeypress(phases:action:)"
---

# onKeyPress(phases:action:)

Performs an action if the user presses any key on a hardware keyboard while the view has focus.

## Declaration

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

```

## Parameters

- `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

Return Value A modified view that binds hardware keyboard input when focused.

## See Also

### Responding to keyboard input

- [onKeyPress(_:action:)](swiftui/view/onkeypress(_:action:).md)
- [onKeyPress(_:phases:action:)](swiftui/view/onkeypress(_:phases:action:).md)
- [onKeyPress(characters:phases:action:)](swiftui/view/onkeypress(characters:phases:action:).md)
- [onKeyPress(keys:phases:action:)](swiftui/view/onkeypress(keys:phases:action:).md)
- [KeyPress](swiftui/keypress.md)
