How can I make the power button on my MacBook run a custom script?
KeyRemap4MacBook can apparently remap the power button for some Macs, but it didn't work with my iMac.

&lt;autogen&gt;__ConsumerToKey__ ConsumerKeyCode::POWER, KeyCode::F19&lt;/autogen&gt;


If you just want to assign a shortcut to a script, see this question.