目标C中Carbon的用户切换通知

时间:2013-05-04 07:54:34

标签: objective-c macos switch-statement

来自https://developer.apple.com/library/mac/#documentation/MacOSX/Conceptual/BPMultipleUsers/Concepts/UserSwitchNotifications.html#//apple_ref/doc/uid/20002210-CJBJDAGF

的代码

演示了如何了解用户切换事件。但我不能将此代码用于用户切换目的,它对我不起作用。 如何在用户切换时使用此代码进行通知?

pascal OSStatus switchEventsHandler (EventHandlerCallRef nextHandler,
                                        EventRef switchEvent,
                                        void* userData)
{
    if (GetEventKind(switchEvent)== kEventSystemUserSessionDeactivated)
    {
        // Perform deactivation tasks here.
    }
    else
    {
        // Perform activation tasks here.
    }

    return noErr;
}

void SwitchEventsRegister()
{
    EventTypeSpec switchEventTypes[2];
    EventHandlerUPP switchEventHandler;

    switchEventTypes[0].eventClass = kEventClassSystem;
    switchEventTypes[0].eventKind = kEventSystemUserSessionDeactivated;
    switchEventTypes[1].eventClass = kEventClassSystem;
    switchEventTypes[1].eventKind = kEventSystemUserSessionActivated;

    switchEventHandler = NewEventHandlerUPP(switchEventsHandler);
    InstallApplicationEventHandler(switchEventHandler, 2,
                                    switchEventTypes, NULL, NULL);

1 个答案:

答案 0 :(得分:0)

您需要使用RunApplicationEventLoop()

启动事件循环

主要可能是这样的:

int main(int argc, char *argv[])
{
    SwitchEventsRegister();

    RunApplicationEventLoop();

    return 0;
}