简体   繁体   English

当Cocoa当前应用程序发生变化时收到通知

[英]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.

相关问题 在Cocoa / Obj-C应用程序中更改网络连接时收到通知 - Getting notified when the network connection changes in a Cocoa / Obj-C application 在OS X的Safari中更改/加载URL时如何通知可可应用程序? - How can a Cocoa application get notified when a URL is changed/loaded in Safari for OS X? 当另一个可可应用程序终止时,一个Cocoa应用程序通知 - One Cocoa app notified when another cocoa app terminated 如何在WebView中通知我的Cocoa应用程序onClick事件? - How can my Cocoa application be notified of onClick events in a WebView? 桌面图像更改时得到通知吗? - Get notified when desktop image changes? 当用户清空垃圾箱时,如何通知我的Cocoa应用程序? - How can my Cocoa app be notified when the user empties the trash? 是否有可以检测当前前台应用程序何时更改的 Swift macOS/Cocoa API? - Is there a Swift macOS/Cocoa API that can detect when the current foreground app changes? 在runtime + cocoa中获取应用程序的路径 - getting the path of a application at runtime + cocoa 在Cocoa应用程序中获取文件夹的路径 - Getting path of a folder in Cocoa application Cocoa - 在应用程序中获取权限 - Cocoa - getting privileges within the application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM