我希望在当前应用程序发生变化时收到通知。我看了一下NSWorkspace。它仅在您自己的应用程序变为活动或丢失活动时才会发送通知。 我希望了解每个应用程序。我怎么能在Cocoa中做到这一点?
答案 0 :(得分:21)
如果您的目标是10.6或更高版本,则会收到通知:
// NSWorkspaceDidActivateApplicationNotification
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(foremostAppActivated:) name:NSWorkspaceDidActivateApplicationNotification object:nil];
答案 1 :(得分:12)
谢谢杰森。 碳事件管理器中的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;
}