I'm working on a MacCatalyst app. I want to listen for escape key when user is editing a UITextField.
I can listen to return key by accepting UITextFieldDelegate with method
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
}
Also the method
func textFieldDidEndEditing(_ textField: UITextField) {
}
gets trigger for both escape and return key, but I am not able to differentiate if its the escape key or return.
Thanks!
This doesn’t seem to be possible with UIKit. I have tried using
UITextFieldDelegate,keyCommands, andpressesBegan, but none of them will report the escape key.But you can catch the escape key if you’re willing to hack your way into AppKit’s local event monitoring using
NSEvent.addLocalMonitorForEvents(matching:handler:).Calling “unavailable” AppKit APIs is pretty ugly, but thankfully mmackh on Github has already built a working solution, which is what I’m using:
If you prefer Swift or want to roll your own solution, check out the very cool Dynamic library, which makes it easy to call “unavailable" AppKit APIs from your UIKit code.