问题
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