2
votes

How do I setup an NSWindows (on OS X) to be closed when the user presses the escape key?

  • The window is presented as a sheet via presentingWindow.beginSheet(mySheetWindow, completionHandler:nil)
  • It has a close button (via IB) that has a key equivalent set to 'escape'. This close button is already working
  • Pressing escape leads to the system audio error output ('Funk' - Sound), but no logging and no closing of the window.
1

1 Answers

3
votes

You have to add a regular button and position it at the top left corner of your window. Set your button width and alpha to 0.

enter image description hereenter image description here

Click at the key equivalent field and press ESC. It will show the escape key icon

enter image description here

Then create your IBAction for the hidden button as follow:

@IBAction func closeWindowAction(sender: AnyObject) {

    window.close()
}

Make sure you have your window outlet connected to the proper window:

@IBOutlet weak var window: NSWindow!