1

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!

1 Answers1

0

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.

Adam
  • 4,405
  • 16
  • 23