mac screensaver start event

不想你离开。 提交于 2019-11-28 17:55:49

问题


Is there an event fired when screensaver starts? Like for keychain locking:

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

回答1:


Finally found it — the solution is to use NSDistributedNotificationCenter and observe folowing events

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

Like

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



回答2:


While there is no Carbon event for this, you can get notified when the current application changes, and then check to see if the new application is the screen saver process.


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



回答3:


This isn't exactly an answer to the question, but I spent a lot of time looking in vain for a list of the notifications posted by OS X, so I wanted to post some code I wrote for notification discovery.

The code simply signs up to listen to all notifications, and prints some info for each as it comes in.

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()


来源:https://stackoverflow.com/questions/1419531/mac-screensaver-start-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!