当当前应用程序在Cocoa中更改时获取通知 [英] Getting notified when the current application changes in Cocoa

查看:199
本文介绍了当当前应用程序在Cocoa中更改时获取通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在当前应用程序更改时收到通知。我看了看NSWorkspace。只有当您自己的应用程序变为活动或失去活动时,它才会发送通知。
我想知道每个应用程序。我可以在Cocoa做这个吗?

I want to be notified when the current application will change. I looked at 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?

推荐答案

谢谢Jason。
在Carbon Event Manager中的kEventAppFrontSwitched是神奇的词

Thank you Jason. kEventAppFrontSwitched in Carbon Event Manager is the magic word

- (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;
}

这篇关于当当前应用程序在Cocoa中更改时获取通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆