简体   繁体   中英

Intercept Command-Quit in Mac App?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM