【发布时间】:2013-10-30 13:31:27
【问题描述】:
按下电源按钮时,我会看到以下对话框,如果已发送此通知:
__CFNotification 0x10011f410 {name = com.apple.logoutInitiated; object = 501}
问题:如何从 C++ 应用程序中侦听此事件并对其采取行动?
仅供参考:
我已经成功破解了一个 Objective C sn-p,它可以做到这一点:
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
@autoreleasepool
{
[[NSDistributedNotificationCenter defaultCenter]
addObserverForName: nil
object: nil
queue: [NSOperationQueue mainQueue]
usingBlock: ^(NSNotification *notification) {
//The event notification we are looking for:
//__CFNotification 0x10011f410 {name = com.apple.logoutInitiated; object = 501}
NSString *event = notification.name;
BOOL res = [event isEqualToString:@"com.apple.logoutInitiated"];
if (res)
{
printf("POWER BUTTON PRESSED");
}
else
{
printf("IGNORE");
}
}];
[[NSRunLoop mainRunLoop] run];
}
}
【问题讨论】:
-
如果我错了,请纠正我,但我相信上面的代码不会拦截,而只是侦听通知。 (细微差别在于“拦截”允许您将通知隐藏到可能也在监听它的其他进程。)
-
是的,抱歉,不需要拦截,只是为了听取此类事件并采取行动。
-
您的 C++ 代码是在 mac 应用程序还是命令行实用程序中?
-
@trojanfoe,我看不出这有什么改变。
-
@QwertyBob 它没有,但我只是想了解可能适用于可能解决方案的限制。