mac屏幕保护程序启动事件

时间:2009-09-14 03:14:05

标签: c objective-c macos events screensaver

屏幕保护程序启动时是否触发了事件?喜欢钥匙串锁定:

OSStatus keychain_locked(SecKeychainEvent keychainEvent, SecKeychainCallbackInfo *info, void *context){...}

3 个答案:

答案 0 :(得分:31)

终于找到了 - 解决方案是使用NSDistributedNotificationCenter并观察以下事件

  • com.apple.screensaver.didstart
  • com.apple.screensaver.willstop
  • com.apple.screensaver.didstop
  • com.apple.screenIsLocked
  • com.apple.screenIsUnlocked

[[NSDistributedNotificationCenter defaultCenter]
   addObserver:self
   selector:@selector(screensaverStarted:)
   name:@"com.apple.screensaver.didstart"
   object:nil];

答案 1 :(得分:2)

虽然没有Carbon事件,但可以在当前应用程序更改时收到通知,然后检查新应用程序是否是屏幕保护程序。


// Register the event handler for when applications change
{
    EventTypeSpec es;
    es.eventClass = kEventClassApplication;
    es.eventKind = kEventAppFrontSwitched;
    InstallApplicationEventHandler(&appChanged, 1, &es, NULL, NULL);
}

OSStatus appChanged(EventHandlerCallRef nextHandler, EventRef anEvent, void* userData)
{
    ProcessSerialNumber psn;    
    GetEventParameter(anEvent, kEventParamProcessID, typeProcessSerialNumber,
                      NULL, sizeof(psn), NULL, &psn);

    // Determine process name
    char procName[255];
    {
        ProcessInfoRec pInfo;
        Str255 procName255;
        FSRef ref;

        pInfo.processInfoLength = sizeof(ProcessInfoRec);
        pInfo.processName = procName255;
        pInfo.processAppRef = &ref;
        GetProcessInformation(&psn, &pInfo);

        const unsigned int size = (unsigned int)procName255[0];
        memcpy(procName, procName255 + 1, size);
        procName[size] = '\0';
    }

    if(strcmp(procName, "ScreenSaverEngine") == 0)
    {
        NSLog(@"Found %s\n", procName);
    }

    return noErr;
}

答案 2 :(得分:1)

这不是问题的答案,但我花了很多时间徒劳地查找OS X发布的通知列表,所以我想发布一些我为通知发现而编写的代码。

代码只需注册即可收听所有通知,并在其输入时为每个打印一些信息。

import Foundation

let distCenter = CFNotificationCenterGetDistributedCenter()
if distCenter == nil {
    exit(1)
}

CFNotificationCenterAddObserver(distCenter, nil, { (center, observer, name, object, userInfo) -> Void in
        print("Event occurred: \(name)  User info: \(userInfo)")
    }, nil, nil, .DeliverImmediately)

CFRunLoopRun()