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.