2009-09-14 4 views
12

¿Se dispara un evento cuando se inicia el protector de pantalla? Al igual que para el bloqueo llavero:evento de inicio del salvapantallas mac

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

Respuesta

28

finalmente lo encontramos - la solución es utilizar NSDistributedNotificationCenter y observar eventos folowing

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

Como

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

Si bien no hay un evento de Carbono para esto, se puede ser notificado cuando los cambios en las aplicaciones actuales, y después comprobar para ver si la nueva aplicación es el proceso de protector de pantalla.


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

Esto no es exactamente una respuesta a la pregunta, pero pasó mucho tiempo buscando en vano una lista de las notificaciones publicadas por OS X, así que quería publicar un código que escribí para el descubrimiento de notificaciones.

El código simplemente se apunta a escuchar a todos los notificaciones, e imprime algo de información para cada medida que llega.

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