This doesn’t seem to be possible with UIKit. I have tried using UITextFieldDelegate
, keyCommands
, and pressesBegan
, 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 targetEnvironment(macCatalyst)
private var keyboardMonitor: IPDFMacEventBusMonitor?
fileprivate func setupKeyboardMonitor() {
let keyboardMonitor = IPDFMacEventBusMonitor(type: .keydown) { event in
guard let event = event else { return nil }
if event.isESC() {
cancel()
return nil
}
return event
}
IPDFMacEventBus.shared().add(keyboardMonitor)
self.keyboardMonitor = keyboardMonitor
}
#endif
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.