拦截命令 - 在Mac App中退出?

时间:2014-06-24 03:16:30

标签: objective-c macos cocoa osx-mavericks

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

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

    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);

还有其他方法吗?感谢。

1 个答案:

答案 0 :(得分:4)

让应用程序委托实现applicationShouldTerminate:方法:

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