2011-06-20 10 views
10

Sé que hay alguna forma de obtener tiempo de inactividad del sistema con el framework IOKit en OS X, pero quiero saber si hay notificaciones disponibles.Notificación cuando el sistema está inactivo o no en OS X

Puedo crear un temporizador para verificar si el tiempo de inactividad es mayor que x, y eso está bien. No importa si detecto el modo inactivo unos segundos más tarde.

El problema es detectar cuando la Mac ya no está inactiva. Quiero que mi aplicación muestre una notificación lo antes posible, no unos segundos más tarde.

¿Hay alguna manera de recibir una notificación al respecto? (IChat parece tener uno)

+3

He probado 'NSWorkspaceScreensDidWakeNotification' y funciona siempre que la definición de regresar de inactividad es la lo mismo que despertar la pantalla. Aparte de eso, es posible que tengas que instalar un toque de evento para detectar eventos de mouse/teclado, interpretándolos como que vuelven de estar inactivos. –

+1

@Bavarious hacen de esto una respuesta. – batman

+1

No sé si hay una diferencia entre Cocoa y Objective C, pero la respuesta a [Objective C: recibe notificaciones sobre el estado inactivo de un usuario] (http://stackoverflow.com/questions/4643579/objective-c-get -notifications-about-a-users-idle-state) podría ayudar. –

Respuesta

8

Esto es de http://developer.apple.com/library/mac/#qa/qa1340/_index.html (de Bill el lagarto comentario)

- (void) receiveSleepNote: (NSNotification*) note 
{ 
    NSLog(@"receiveSleepNote: %@", [note name]); 
} 

- (void) receiveWakeNote: (NSNotification*) note 
{ 
    NSLog(@"receiveWakeNote: %@", [note name]); 
} 

- (void) fileNotifications 
{ 
    //These notifications are filed on NSWorkspace's notification center, not the default 
    // notification center. You will not receive sleep/wake notifications if you file 
    //with the default notification center. 
    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
      selector: @selector(receiveSleepNote:) 
      name: NSWorkspaceWillSleepNotification object: NULL]; 

    [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self 
      selector: @selector(receiveWakeNote:) 
      name: NSWorkspaceDidWakeNotification object: NULL]; 
} 
1
NSTimeInterval GetIdleTimeInterval() { 

    io_iterator_t iter = 0; 
    int64_t nanoseconds = 0; 

    if (IOServiceGetMatchingServices(kIOMasterPortDefault, IOServiceMatching("IOHIDSystem"), &iter) == KERN_SUCCESS) { 
     io_registry_entry_t entry = IOIteratorNext(iter); 
     if (entry) { 
      CFMutableDictionaryRef dict; 
      if (IORegistryEntryCreateCFProperties(entry, &dict, kCFAllocatorDefault, 0) == KERN_SUCCESS) { 
       CFNumberRef obj = CFDictionaryGetValue(dict, CFSTR("HIDIdleTime")); 
       if (obj) 
        CFNumberGetValue(obj, kCFNumberSInt64Type, &nanoseconds); 
       CFRelease(dict); 
      } 
      IOObjectRelease(entry); 
     } 
     IOObjectRelease(iter); 
    } 

    return (double)nanoseconds/1000000000.0; 
} 
Cuestiones relacionadas