Can the power button of a PC or laptop be remapped?

Solution 1:

As James points out, the power button is generally mapped at quite a low level to the BIOS so that it should always work even if the OS fails. It is very annoying when this fails since you then have to mess about with power cables. It is more annoying on a laptop as you have to mess with power cables and the battery. It is even (!) more annoying on a modern, lightweight laptop because the batteries are not removable!

Linux, however is pretty "hacker" friendly and tries to expose as much as possible of the inner workings of the hardware to you. Also, all OS's need to do "stuff" before closing down. So all of that is a balancing act. Generally, on modern PC's, pressing the button once will trigger a fairly soft event into the OS so that the OS can shut down gracefully. However holding down the button will, after a few seconds, trigger a BIOS level event to cut the power.

So you should be able to trap the soft power event in any OS if you have the skills to do so. This is relatively easy in Linux because the soft event triggers a sequence of further events that close down the higher levels of the OS (e.g. the desktop GUI), moving to lower levels until it is safe to power off. If you investigate "runlevels", you will doubtless find more details on how to do things at the most appropriate point.

In Windows, you can use Windows Task Scheduler to run tasks on different events. So you could run something "On disconnect from user session" which should trigger as power down is happening (I've not actually tried it). Or you can look through your event logs to see which event(s) might be triggered that you can attach to.