How to detect when the power button is pressed in cocoa?


I'm trying to figure out how to detect when the user presses the power key on the keyboard.

I'm developing a kiosk application and I want to run a routine when the user want to force shut down the computer by pressing and holding the power key.

Thank you in advance.

There are no methods that will be called when a user presses and holds the power key. That's directly handled in hardware and doesn't even get routed to the OS, much less to your application - it's the equivalent of pulling the power cord.

Your best bet is to create a "clean shut down" user preference key or temporary file that your app will remove as part of its normal shutdown procedure. When your app starts up, it can then check for it to see if the previous run was shut down normally, or had the rug yanked out from under it.

Oh, and since this is a kiosk - don't give users physical access to the power switch to begin with. Sometimes the simplest solutions are best!