Mac应用程序中拦截命令退出? [英] Intercept Command-Quit in Mac App?

查看:412
本文介绍了Mac应用程序中拦截命令退出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为Mac(Mavericks)制作一个不处理命令退出选项的应用程序。

I would like to make an app for Mac (Mavericks) that does not handle the command-quit option.

我找到了以下解决方案,但它必须是因为我收到错误:

I found the following solution but it must be out of date because I get an error:

    CFMachPortRef eventTap = CGEventTapCreate(kCGHIDEventTap,
                                          kCGHeadInsertEventTap,
                                          kCGEventTapOptionDefault,
                                          CGEventMaskBit(kCGEventKeyDown),
                                          &KeyDownCallback,
                                          NULL);

CFRunLoopSourceRef runLoopSource = CFMachPortCreateRunLoopSource(kCFAllocatorDefault, eventTap, 0);
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopCommonModes);
CFRelease(runLoopSource);
CGEventTapEnable(eventTap, true);

另一种方法?感谢。

推荐答案

只要让应用程式委托代码实作 applicationShouldTerminate: 方法:

Just have your application delegate implement the applicationShouldTerminate: method:

- (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender
{
    // work out whether to actually quit or not
    BOOL shouldQuit = /* insert logic here */;
    if (shouldQuit)
        return NSTerminateNow;
    else
        return NSTerminateCancel;
}

这篇关于Mac应用程序中拦截命令退出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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