2011-06-20 11 views
10

पर निष्क्रिय है या नहीं, तो मुझे पता है कि ओएस एक्स पर आईओकेट फ्रेमवर्क के साथ सिस्टम निष्क्रिय समय प्राप्त करने का कोई तरीका है, लेकिन मैं जानना चाहता हूं कि अधिसूचनाएं उपलब्ध हैं या नहीं।जब सिस्टम ओएस एक्स

मैं एक टाइमर बना सकता हूं यह जांचने के लिए कि निष्क्रिय समय x से अधिक है, और यह ठीक है। इससे कोई फर्क नहीं पड़ता कि मैं कुछ सेकंड बाद निष्क्रिय मोड का पता लगाता हूं।

समस्या यह पता लगाना है कि मैक अब निष्क्रिय नहीं है। मैं चाहता हूं कि मेरा ऐप जल्द से जल्द अधिसूचना दिखाए, कुछ सेकंड बाद नहीं।

क्या इसके लिए कोई सूचना नहीं है? (IChat एक है लगता है)

+3

मैं परीक्षण किया है 'NSWorkspaceScreensDidWakeNotification' (बिल छिपकली टिप्पणी से) है और यह बेकार से लौट रहे है की परिभाषा प्रदान की काम करता है प्रदर्शन जागने के समान ही। इसके अलावा, आपको माउस/कीबोर्ड ईवेंट का पता लगाने के लिए एक ईवेंट टैप इंस्टॉल करना पड़ सकता है, जो उन्हें निष्क्रिय से लौटने के रूप में व्याख्या करता है। –

+1

@Bavarious यह एक उत्तर बनाओ। – batman

+1

मुझे नहीं पता कि कोको और ऑब्जेक्टिव सी के बीच कोई अंतर है, लेकिन इसका जवाब [उद्देश्य सी: उपयोगकर्ता के निष्क्रिय राज्य के बारे में अधिसूचनाएं प्राप्त करें] (http://stackoverflow.com/questions/4643579/objective-c-get -नोटिफिकेशन-ए-यूजर-निष्क्रिय-स्टेट) मदद कर सकता है। –

उत्तर

8

यह http://developer.apple.com/library/mac/#qa/qa1340/_index.html से

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