在Cocoa应用程序中处理Cmd-Q(和菜单项“以编程方式退出应用程序”) [英] Handle Cmd-Q in Cocoa Application (and Menu Item 'Quit Application' programmatically)

查看:169
本文介绍了在Cocoa应用程序中处理Cmd-Q(和菜单项“以编程方式退出应用程序”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个只有一个窗口的游戏应用程序。应用程序在没有.xib文件的帮助下创建,如下所述:如何创建一个GUI并以编程方式对Cocoa事件做出反应?

I have created a gaming application that has only one window. Application is created without a help of .xib files as described here: How can I create a GUI and react to Cocoa events programatically?

现在,我可以捕获标准的'事件在应用程序的主循环中:

Now, I can catch standard 'key up/down' events in application's main loop:

 NSEvent* event = [NSApp nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantPast] inMode:NSDefaultRunLoopMode dequeue:YES];
 NSEventType eventType = [event type];
 if (eventType == NSKeyDown)
 {
    my_uint32 keycode = [event keyCode];
    input::doSomeWork(keycode);
 }

此外,我可以正确退出应用程序,窗口中输入以下代码:

Also, I can properly quit an application when a red cross is pressed on the window with the following code:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    g_myEngine.stop();
    return NSTerminateNow;
}

但我如何:

a)当选择菜单项退出MyApplicationName时捕获?

a) Catch when a menu item 'Quit MyApplicationName' is chosen?

b)处理Cmd-Q事件?

b) Handle Cmd-Q event?

更新:
我已经添加了此代码:

update: I have added this code:

id menubar = [[NSMenu new] autorelease];
id appMenuItem = [[NSMenuItem new] autorelease];
[menubar addItem:appMenuItem];
[NSApp setMainMenu:menubar];
id appMenu = [[NSMenu new] autorelease];
id appName = [[NSProcessInfo processInfo] processName];
id quitTitle = [@"Quit " stringByAppendingString:appName];
id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
[appMenu addItem:quitMenuItem];
[appMenuItem setSubmenu:appMenu];

现在应用程序退出菜单,但Cmd-Q仍然无法工作。

and now application exits from the menu, but Cmd-Q still does not work.

推荐答案

尝试设置菜单项的键等效修饰符掩码

不要忘记添加关闭窗口,最小化,隐藏,进入/退出全屏等菜单项,加上整个编辑菜单,包括所有文本编辑功能,当前和未来。 (为什么是编辑菜单?我假设,至少,你会让用户命名他们的保存游戏,他们的高分(如果适用)或他们的性格。如果你有任何文本编辑<在应用程序中,您应该支持完整的编辑菜单。)

Don't forget to also add menu items for Close Window, Minimize, Hide, Enter/Exit Full Screen, etc., plus the entire Edit menu, including all text-editing features, current and future. (Why the Edit menu? I assume that, at the least, you'll enable the user to name their save games, their high scores (if applicable), or their character. If you have any text editing anywhere in the application, you should support the full Edit menu.)

这篇关于在Cocoa应用程序中处理Cmd-Q(和菜单项“以编程方式退出应用程序”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆