我希望在当前应用程序发生变化时收到通知.我看了一下NSWorkspace.它仅在您自己的应用程序变为活动或丢失活动时才会发送通知.我希望了解每个应用程序.我怎么能在Cocoa中做到这一点?
如果您的目标是10.6或更高版本,则会收到以下通知:
// NSWorkspaceDidActivateApplicationNotification [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(foremostAppActivated:) name:NSWorkspaceDidActivateApplicationNotification object:nil];
Apple文档:http://developer.apple.com/library/mac/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-SW97
谢谢杰森.碳事件管理器中的kEventAppFrontSwitched是一个神奇的词
- (void) setupAppFrontSwitchedHandler { EventTypeSpec spec = { kEventClassApplication, kEventAppFrontSwitched }; OSStatus err = InstallApplicationEventHandler(NewEventHandlerUPP(AppFrontSwitchedHandler), 1, &spec, (void*)self, NULL); if (err) NSLog(@"Could not install event handler"); } - (void) appFrontSwitched { NSLog(@"%@", [[NSWorkspace sharedWorkspace] activeApplication]); }
和处理程序
static OSStatus AppFrontSwitchedHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData) { [(id)inUserData appFrontSwitched]; return 0; }