Monitoring Screensaver Events in OSX

后端 未结 3 1566
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-02 16:45

I want to monitor screensaver and lockscreen events on an OSX box. As a first pass, I\'m fine with them just printing to the console.

Following the advice of another\'s

3条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-02 17:15

    It appears the strategy you're trying to use for this won't work, because the com.apple.screensaver.* notifications are no longer supported.
    In the replies to this equivalent question, it is mentioned that: 'for Snow Leopard the screenIsLocked and screenIsUnlocked notifications aren't available anymore.'
    You can register for screen sleeps, which of course is not the same but may be an acceptable alternative for you, by listening to the NSWorkspaceScreensDidSleepNotification notification, or to the computer going to sleep by listening to the NSWorkspaceWillSleepNotification. Sample code can be found on this forum.

    Sidenote: if you use nil for the name, you will receive lots of events:

    [center addObserver:self
               selector:@selector(receive:)
                   name:nil
                 object:nil
    ];
    

    If you do that, you will see that you're doing everything basically right, because you will receive all kind of events - but no screensaver ones.

提交回复
热议问题