[英]Getting notified when the current application changes in Cocoa
I want to be notified when the current application will change. 我希望在当前应用程序发生变化时收到通知。 I looked at NSWorkspace. 我看了一下NSWorkspace。 It will send notifications only when your own application becomes active or loses the activity. 它仅在您自己的应用程序变为活动或丢失活动时才会发送通知。 I want to be informed about every application. 我希望了解每个应用程序。 How can I do this in Cocoa? 我怎么能在Cocoa中做到这一点?
If you're targeting 10.6 or later there's a notification for this: 如果您的目标是10.6或更高版本,则会收到以下通知:
// NSWorkspaceDidActivateApplicationNotification
[[[NSWorkspace sharedWorkspace] notificationCenter] addObserver:self selector:@selector(foremostAppActivated:) name:NSWorkspaceDidActivateApplicationNotification object:nil];
Apple docs: http://developer.apple.com/library/mac/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-SW97 Apple文档: http : //developer.apple.com/library/mac/DOCUMENTATION/Cocoa/Reference/ApplicationKit/Classes/NSWorkspace_Class/Reference/Reference.html#//apple_ref/doc/uid/20000391-SW97
Thank you Jason. 谢谢杰森。 kEventAppFrontSwitched in Carbon Event Manager is the magic word 碳事件管理器中的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]);
}
And the handler 和处理程序
static OSStatus AppFrontSwitchedHandler(EventHandlerCallRef inHandlerCallRef, EventRef inEvent, void *inUserData)
{
[(id)inUserData appFrontSwitched];
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.