0

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

demonstrates how to be aware of user switch event . But i can't use this code for user switch purpose ,and it doesn't work for me. How to use this code to be notified when user switch occurs?

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);
4

1 回答 1

0

您需要使用启动事件循环RunApplicationEventLoop()

主要可能是这样的:

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

    RunApplicationEventLoop();

    return 0;
}
于 2014-03-06T19:31:48.923 回答