2

I am working on Win/MacOS game app. When user closes the app, I want to show a confirmation popup sorta "Are you sure you want to leave?".

On Windows, I listen to WM_CLOSE message to intercept Alt-F4. Then, if user selects "Yes, I want to leave" -- I call PostQuitMessage(0); to exit the app.

How do I intercept Cmd+Q on Mac OS? And, if user selects "Yes, I want to leave" -- how do I close the app (i.e. how to perform the same action as Cmd+Q does by default)?

MacOS part is in written in plain Objective-C and I am looking for an answer in Objective-C.

Nick
  • 3,205
  • 9
  • 57
  • 108
  • Possible duplicate of [Swing on OSX: How to Trap command-Q?](http://stackoverflow.com/questions/2061194/swing-on-osx-how-to-trap-command-q) – BladeMight Feb 04 '17 at 11:16

1 Answers1

3

Implement the delegate method applicationShouldTerminate of NSApplication and show a custom modal alert. Depending on the answer return NSTerminateNow, NSTerminateCancel or NSTerminateLater.

In case of NSTerminateLater you can later call [NSApp replyToApplicationShouldTerminate:YES]; to finally quit the app.

vadian
  • 274,689
  • 30
  • 353
  • 361