Stop my app from quitting!

I’m trying to stop my app from quitting. It’s currently responding to Command-Q and I can’t seem to intercept it. It’s an app that runs without a MenuBar and without a Dock icon - it’s basically an NSStatusBar app. One of the NSStatusItem’s menu items opens a preferences window and at this point, it’s possible to quit the app with the Command-Q keyboard shortcut.

Apple haven’t documented this but the accepted behavior (going by a few highly regarded MAS NSStatusBar apps) seems to be that a LSUIElement app doesn’t quit by any method other than choosing Quit from the NSStatusItem menu.

So why is my window receiving the Command-Q shortcut? How can I stop it?

Use the cancelclose event to intercept this.

Sure but the problem is that I can’t intercept Cmd-Q, so I can’t check in CancelClose if the app is quitting from the menu bar (which would be fine) or from a keyboard shortcut (which I don’t want).

OK, I am confused.
The Cancelclose event from the main app is triggered whenever you try to quit the app (Cdm+Q, via menubar ‘quit’, …)
The only way to bypass the main cancelclose event is doing a Force Quit. :slight_smile:

[quote=31151:@Christoph De Vocht]OK, I am confused.
The Cancelclose event from the main app is triggered whenever you try to quit the app (Cdm+Q, via menubar ‘quit’, …)[/quote]
The user has to be able to quit the app from the menu bar.
If the app has no window open, Cmd-Q (naturally in this case) does nothing.
But if the app has a window open, Cmd-Q quits the app and that’s incorrect behavior.

Do you know what I mean?

Have you tried checking if Keyboard.AsyncCommandKey is true in CancelClose? That would most certainly indicate where the event came from.

Don’t think that is possible (only quiting from menu bar).

Greg, didn’t think of that - works great, thanks.

So, just to satisfy my OCD side - why is my app still receiving the Cmd-Q shortcut at all?

I suspect the OS is intercepting and forwarding the event to your app, but we’d need to ask Joe.

[quote=31153:@Gavin Smith]The user has to be able to quit the app from the menu bar.
If the app has no window open, Cmd-Q (naturally in this case) does nothing.
But if the app has a window open, Cmd-Q quits the app and that’s incorrect behavior.

Do you know what I mean?[/quote]
I don’t know about “incorrect behavior”. I’m starting to see a lot of apps fully quits on Cmd-Q, even those found in the Mac App Store. I know I get irritated at times when I want to quit an app after hitting that Cmd-Q and the app really isn’t closed. :wink:

Gavin, I don’t like checking for the cmd key because it’s not foolproof.

I believe that there’s a cleaner way to do that. I’ve done so in my own LSUIElement app (iClip).

Try this: Provide your own Menu handler for the Quit menu in your App class, and just return true from it. That allows you to properly intercept cmd-Q.

Does that help? If not, ask again and I’ll look into my source code to figure out what else must be done.

[quote=31200:@Thomas Tempelmann]Gavin, I don’t like checking for the cmd key because it’s not foolproof.

I believe that there’s a cleaner way to do that. I’ve done so in my own LSUIElement app (iClip).

Try this: Provide your own Menu handler for the Quit menu in your App class, and just return true from it. That allows you to properly intercept cmd-Q.

Does that help? If not, ask again and I’ll look into my source code to figure out what else must be done.[/quote]

Thomas, that is cleaner and satisfies my OCDness - sorted, thanks.