Skip to content

Implement OnKeyPress#45

Open
DandyLyons wants to merge 1 commit intorensbreur:mainfrom
DandyLyons:onKeyPress
Open

Implement OnKeyPress#45
DandyLyons wants to merge 1 commit intorensbreur:mainfrom
DandyLyons:onKeyPress

Conversation

@DandyLyons
Copy link

@DandyLyons DandyLyons commented Oct 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.

@DandyLyons DandyLyons marked this pull request as ready for review November 2, 2024 15:34
rosswaredev added a commit to rosswaredev/SwiftTUI that referenced this pull request Jan 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants