0

I am writing a simple menu bar application for MacOS using SwiftUI. I would like the application to respond to the escape key. Using what I have pieced together so far, I have something like this:

extension NSWindow {
    open override func keyDown(with event: NSEvent) {
        print("keyDown: \(event.keyCode)")
    }
    open override func keyUp(with event: NSEvent) {
        print("keyUp: \(event.keyCode)")
    }
}


class AppDelegate: NSObject, NSApplicationDelegate {
    func applicationDidFinishLaunching(_ aNotification: Notification) {

    //  etc

    }
}

The AppDelegate creates and launches a NSPopover.

When I run the application, I do get the keyUp messages, but, not the keyDown for some reason.

The question is how can I respond to the key from within the AppDelegate? Ultimately, I want to close the popover using the escape key, but I would also like to explore other possibilities.

Manngo
  • 14,066
  • 10
  • 88
  • 110
  • See this for example https://stackoverflow.com/a/61278596/12299030 – Asperi Sep 16 '20 at 07:53
  • @Asperi Thanks for the information. I’ve had no success so far in fitting it in. I think it may be because I’m using a popup. – Manngo Sep 16 '20 at 23:19
  • @Asperi OK, I’ve got it working now. I still can’t work out why `keyDown` isn’t working. Would you mind if I answered my own question with a simplified version of your code? – Manngo Sep 17 '20 at 00:42

0 Answers0