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

5,820

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.

Share:
5,820

Related videos on Youtube

glS
Author by

glS

Updated on September 18, 2022

Comments

  • glS
    glS almost 2 years

    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.

    • Ctrl-alt-dlt
      Ctrl-alt-dlt about 9 years
      You can change what the power button does in terms of shutdown/hibernate/sleep but at the end of the day, all that button does is short two pins on the motherboard so you are limited to what those pins can be programmed to do, which i expect isn't much.
    • glS
      glS about 9 years
      That seems interesting, could you expand on that point? Also I notice that for example on a Linux tty a series of operations are performed while the power button is shutting down the system, which makes me think that something CAN be changed in what the power button does, at software level.
    • Ctrl-alt-dlt
      Ctrl-alt-dlt about 9 years
      Well those two pins will send a very low level message to the CPU, possibly through the on-board chip-set. You won't be able to change this message. I imagine it will be binary up to the point where it is compiled by the CPU. You could with some expertise, potentially change how the CPU responds to the message on a higher level than the binary machine code.