在 OSX 中监视屏幕保护程序事件 [英] Monitoring Screensaver Events in OSX

查看:22
本文介绍了在 OSX 中监视屏幕保护程序事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 OSX 机器上监视屏幕保护程序和锁屏事件.作为第一遍,我对它们只是打印到控制台没问题.

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.

按照他人问题的建议,我写了一些Objective C来监听Cocoa通知为了com.apple.screensaver.didstartcom.apple.screensaver.didstopcom.apple.screenIsLockedcom.apple.screenIsUnlocked 事件.

Following the advice of another's question, I wrote some Objective C to listen for Cocoa Notifications for the com.apple.screensaver.didstart, com.apple.screensaver.didstop, com.apple.screenIsLocked, and com.apple.screenIsUnlocked events.

// ScreenSaverMonitor.h
#import <Foundation/NSObject.h>
#import <Foundation/NSNotification.h>

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

// ScreenSaverMonitor.m
#import "ScreenSaverMonitor.h"
#import <Foundation/NSString.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import <Foundation/NSRunLoop.h>
#import <stdio.h>

@implementation ScreenSaverMonitor
-(id) init {
  NSDistributedNotificationCenter * center 
    = [NSDistributedNotificationCenter defaultCenter];

  [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
  ];
  printf("running loop... (^C to quit)");
  [[NSRunLoop currentRunLoop] run];
  printf("...ending loop");
  return self;
}
-(void) receive: (NSNotification*) notification {
  printf("%s
", [[notification name] UTF8String] );
}
@end

// ScreenSaverMonitorMain.m
#import "ScreenSaverMonitor.h"

int main( int argc, char ** argv) {
  [[ScreenSaverMonitor alloc] init];
  return 0;
}

它编译得很好,但是当我运行它时,我似乎没有观察到任何屏幕保护程序事件(尽管屏幕保护程序出现多次):

It compiles fine, but when I run it, I don't seem to observe any screensaver events (despite having the screensaver come on multiple times):

% gcc -Wall ScreenSaverMonitor.m ScreenSaverMonitorMain.m -o ScreenSaverMonitor -lobjc -framework Cocoa
% ./ScreenSaverMonitor
running loop (^C to quit)...
^C
%

我的 Objective C 和 Cocoa 知识非常生疏,所以我不确定我是否使用了错误的框架,或者我是否注册了错误的事件(也不知道去哪里查看这些是否是错误的)正确的事件与否).

My Objective C and Cocoa knowledge is very rusty, so I'm not sure if I'm using the framework wrong, or if I've registered for the wrong events (nor where to look to find out whether those are the right events or not).

那我做错了什么?

推荐答案

您已经评论了您的问题.

You've commented your problem already.

while(1); // busy wait's bad, I know, but easy to implement

以上几乎总是一个坏主意.

The above is almost ALWAYS a bad idea.

NSDistributedNotificationCenter 实际上需要一个正在运行的主线程 NSRunLoop 来操作.

NSDistributedNotificationCenter actually requires a running main thread NSRunLoop to operate.

http://developer.apple.com/library/ios/#documentation/cocoa/Conceptual/Notifications/Articles/NotificationCenters.html#//apple_ref/doc/uid/20000216-BAJGDAFC

从 OS X 上的命令行应用程序的 main() 创建和旋转运行循环是一件相当简单的事情.快速搜索可以找到大量示例.

Creating and spinning a run loop from the main() of a command line application on OS X is a fairly simple thing to do. There are plenty of examples available with a quick search.

这篇关于在 OSX 中监视屏幕保护程序事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆