How to show a menu when I press the power key from the motherboard?

my customer want me to show a customize menu when press the power key from the motherboard. Does anyone have some advice or is there any document about this?

thank you very much.

It depends, there are several ways this could be done based on your system.

If you have logind and are trying to do this in a way that could work with any desktop, you will have to create an inhibitor with handle-power-key and then listen for the power key event with evdev. Without logind or similar, you should be able to just open the evdev device, but I think that will require elevated privileges. On the systems I have seen there usually is a special device for just the power button with udev tag power-switch that you will want to open. The event you are looking for is a key event with keycode KEY_POWER. If you don’t have logind then you may also be able to use acpid2 to handle this for you. For non-Linux systems without evdev then I’m not sure.

However if you are using GNOME shell, you will probably also have to set GNOME’s “Power Button Behavior” in the power settings panel to “nothing” in order to block GNOME from handling the event.

If you want to show the menu in the shell, the keycode in xkb is XF86PowerOff. It may be possible to skip interacting with logind/evdev and build an extension that intercepts that before it is handled by the rest of the shell. But I haven’t tried that.

Sorry I don’t have more info but this is somewhat of a low level operation.

thank you very much, this is helpful.

can I ask one more question? Do you know how does GNOME’s “Power Button Behavior” change the action of pwoer key?

The behavior is hard coded in gsd-media-keys: gsd-media-keys-manager.c#L2043

Thank you very much!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.