Monitoring Screensaver Events in OSX

后端 未结 3 1533
佛祖请我去吃肉
佛祖请我去吃肉 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条回答
  •  梦毁少年i
    2021-02-02 17:32

    Edit: further testing showed that com.apple.screensaver.didlaunch is also available, the code here is tested on 10.6.8 and 10.8.4

    Your code will work if you remove [[NSRunLoop currentRunLoop] run]; and initialize ScreenSaverMonitor from a Cocoa application's applicationDidFinishLaunching: method. ( Just create a new Cocoa application project in XCode and add your code where appropriate ).

    ScreenSaverMonitor.h

    #import 
    
    @interface ScreenSaverMonitor : NSObject
    -(id) init;
    -(void) receive: (NSNotification*) notification;
    
    @end
    

    ScreenSaverMonitor.m

    #import "ScreenSaverMonitor.h"
    #import 
    #import 
    #import 
    #import 
    
    @implementation ScreenSaverMonitor
    -(id) init {
        NSDistributedNotificationCenter * center
        = [NSDistributedNotificationCenter defaultCenter];
    
        [center addObserver: self
                   selector:    @selector(receive:)
                       name:        @"com.apple.screensaver.didlaunch"
                     object:      nil
         ];
    
        [center addObserver: self
                   selector:    @selector(receive:)
                       name:        @"com.apple.screensaver.didstart"
                     object:      nil
         ];
        [center addObserver: self
                   selector:    @selector(receive:)
                       name:        @"com.apple.screensaver.didstop"
                     object:      nil
         ];
        [center addObserver: self
                   selector:    @selector(receive:)
                       name:        @"com.apple.screenIsLocked"
                     object:      nil
         ];
        [center addObserver: self
                   selector:    @selector(receive:)
                       name:        @"com.apple.screenIsUnlocked"
                     object:      nil
         ];
        return self;
    }
    -(void) receive: (NSNotification*) notification {
        printf("%s\n", [[notification name] UTF8String] );
    }
    
    @end
    

    AppDelegate.h

    #import 
    #import "ScreenSaverMonitor.h"
    
    @interface AppDelegate : NSObject 
    
    @property (assign) IBOutlet NSWindow *window;
    @property (retain) ScreenSaverMonitor *monitor;
    @end
    

    AppDelegate.m

    #import "AppDelegate.h"
    #import "ScreenSaverMonitor.h"
    
    @implementation AppDelegate
    @synthesize monitor;
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
    {
        // Insert code here to initialize your application
        self.monitor = [[ScreenSaverMonitor alloc] init];
    
    }
    
    @end
    

    main.m

    #import 
    
    int main(int argc, char *argv[])
    {
        return NSApplicationMain(argc, (const char **)argv);
    }
    

提交回复
热议问题