Nick
Nick

Reputation: 3461

How to intercept Cmd+Q

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.

Upvotes: 2

Views: 673

Answers (1)

vadian
vadian

Reputation: 285250

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.

Upvotes: 3

Related Questions