Implement OnKeyPress #6

Open
opened 2025-10-14 15:43:12 -06:00 by navan · 0 comments
Owner

Originally created by @DandyLyons on 10/31/2024

This PR resolves issue #41 .

It attempts to mimic the onKeyPress API from the original SwiftUI API. See https://developer.apple.com/documentation/swiftui/view/onkeypress(_:action:)

So far it is a proof of concept. It has not been tested extensively.

Certain design decisions have not been made. For example, if multiple views subscribe to the same onKeyPress then should they both work, one of them, which should have precedence.

Known Limitation

It is not recommended to bind onKeyPress to any of the following keys because they will conflict with the already existing implementation of Button:

  • any arrow keys
  • space bar (" ")
  • return/enter key ("\n")

Room For Improvement

The SwiftUI API includes a convenience type named KeyEquivalent which is a simple name space defining Character constants. See https://developer.apple.com/documentation/swiftui/keyequivalent

I thought it unnecessary to add this type to SwiftTUI since it already includes its own Character constant type (ASCII) and their's no need to conflict with that.

In the future it might be worth considering unifying the API by either changing onKeyPress(:,action:) to accept an ASCII or to rename ASCII to KeyEquivalent so that it better matches the SwiftUI API.

*Originally created by @DandyLyons on 10/31/2024* This PR resolves issue #41 . It attempts to mimic the `onKeyPress` API from the original SwiftUI API. See https://developer.apple.com/documentation/swiftui/view/onkeypress(_:action:) So far it is a proof of concept. It has not been tested extensively. Certain design decisions have not been made. For example, if multiple views subscribe to the same `onKeyPress` then should they both work, one of them, which should have precedence. ## Known Limitation It is not recommended to bind `onKeyPress` to any of the following keys because they will conflict with the already existing implementation of `Button`: - any arrow keys - space bar (`" "`) - return/enter key (`"\n"`) ## Room For Improvement The SwiftUI API includes a convenience type named `KeyEquivalent` which is a simple name space defining `Character` constants. See https://developer.apple.com/documentation/swiftui/keyequivalent I thought it unnecessary to add this type to SwiftTUI since it already includes its own Character constant type (`ASCII`) and their's no need to conflict with that. In the future it might be worth considering unifying the API by either changing `onKeyPress(:,action:)` to accept an `ASCII` or to rename `ASCII` to `KeyEquivalent` so that it better matches the SwiftUI API.
Sign in to join this conversation.
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github/SwiftTUI#6
No description provided.