【问题标题】:User Switch Notification by Carbon in Objective CObjective C 中 Carbon 的用户切换通知
【发布时间】:2013-04-28 15:03:14
【问题描述】:

来自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);

【问题讨论】:

  • 如果您使用的是 Objective-C,为什么要使用 Carbon 而不是 Cocoa?

标签: objective-c macos switch-statement


【解决方案1】:

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

主要可能是这样的:

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

    RunApplicationEventLoop();

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多