Saturday, 17 August 2019

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


Forget for a second how bad an idea this would be. But is it possible to remap the power button of a PC or laptop to, say, print some message or anything other than managing the power?


If not, why? Who handles that button, and how low-level such a modification would have to be?


I'm also interested in how the answer changes in the various systems, if it does.



Answer



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.


No comments:

Post a Comment

How can I VLOOKUP in multiple Excel documents?

I am trying to VLOOKUP reference data with around 400 seperate Excel files. Is it possible to do this in a quick way rather than doing it m...