当按下电源按钮时,我会看到以下对话框,如果发送了此通知:
__CFNotification 0x10011f410 {name = com.apple.logoutInitiated; object = 501}
问题:如何从 C++ 应用程序中侦听此事件并对其采取行动?
供参考:
我设法拼凑了一个 Objective C 代码片段,它可以做到这一点:
#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];
}
}