Intercept Command-Quit in Mac App?

北战南征 提交于 2020-01-07 05:26:26

问题


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

Another other ways? Thanks.


回答1:


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


来源:https://stackoverflow.com/questions/24377811/intercept-command-quit-in-mac-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!