Knowing when the system has gone to sleep in a Menu Extra?

∥☆過路亽.° 提交于 2019-12-07 04:17:03

问题


I developed a Menu Extra (menulet) for the Mac but I'd like to know if the machine using the Menu Extra has gone to sleep. I implemented a snooze function but since it's a time-based method it's rather unreliable. Any hints or advice would be greatly appreciated!

Thanks!


回答1:


You probably want to check out NSWorkspaceWillSleepNotification in NSWorkspace.
The trick is, you need to register for the notifications on NSWorkspace's notificationCenter, not the default one. See Technical Q&A QA1340.

Sample code:

- (void) receiveSleepNote: (NSNotification*) note
{
    NSLog(@"receiveSleepNote: %@", [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:nil];
}


来源:https://stackoverflow.com/questions/6089096/knowing-when-the-system-has-gone-to-sleep-in-a-menu-extra

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