1

I have MacBook Air Retina(TouchID) and I need to listen power button pressing. How I do that?

I have tried this solution Listen for power button event in OS X but didn't work:

#include <iostream>
#include <CoreFoundation/CoreFoundation.h>

void
myCallBack(CFNotificationCenterRef center,
           void *observer,
           CFStringRef name,
           const void *object,
           CFDictionaryRef userInfo) {
    std::cout << "Power Button Pressed" << std::endl;
}

int
main(int argc, const char * argv[])
{
    CFNotificationCenterRef distCenter;
    CFStringRef evtName = CFSTR("com.apple.logoutInitiated");
    distCenter = CFNotificationCenterGetDistributedCenter();
    if (NULL == distCenter)
        return 1;
    CFNotificationCenterAddObserver(distCenter, NULL, &myCallBack, evtName, NULL, CFNotificationSuspensionBehaviorDeliverImmediately);
    CFRunLoopRun();
    return 0;
}
273K
  • 29,503
  • 10
  • 41
  • 64
Emre Şenyuva
  • 11
  • 1
  • 4

1 Answers1

0

That mentioned solution is obsolete. Try this event name:

CFStringRef evtName = CFSTR("com.apple.system.loginwindow.shutdownInitiated");
273K
  • 29,503
  • 10
  • 41
  • 64