I'm building a SwiftUI app with a UITextView subclass, and it seems that the software keyboard doesn't trigger the pressesBegan
or pressesEnded
functions of UITextView. With a hardware keyboard, pressesBegan
works as expected, allowing us to intercept key presses in our subclass.
I can't find any documentation about this, or any other forum posts (here or on Stack Overflow) that talk about a discrepancy between software and hardware keyboard behaviors, and I can't believe this is an intended behavior. Our app is a SwiftUI app, in case that's relevant.
Does anyone have any guidance? Is this a bug or am I not understanding this API? Any information or work arounds would be greatly appreciated.
I've made a sample project that demonstrates this issue, which you can grab from GitHub at https://github.com/nyousefi/KeyPressSample. To see this in action, run the sample project and start pressing keys. The hardware keyboard will print the key press at the top of the screen (above the text view), while the software keyboard won't.
This behaves as intended. pressesBegan
is intended to be used for hardware keyboard presses. For example, you might use this to support WASD movement in a game, or detecting when modifier keys are held down to perform alternate actions in a Mac Catalyst app.
The software keyboard is intended to be used for text editing/typing, and does not send hardware keyboard events the same way.
If you're still interested in these events from software keyboard presses, I'd be interested in hearing your use case (in a feedback report, of course).