How to change mapping for the «PowerOff» key on keyboard?
Solution 1
I was able to get it working on my ASUS N751JK in Ubuntu 18.04 LTS and Ubuntu 16.04 LTS (tested with Unity desktop environment in both cases and KDE Plasma in 18.04):
-
First of all, set action of "Power Off" button to
nothing
.Unity
Like mentioned in answer of Johano Fierra:
gsettings set org.gnome.settings-daemon.plugins.power button-power "nothing"
or alternatively
sudo apt install dconf-tools
and usedconf-editor
to changeorg.gnome.settings-daemon.plugins.power
property (like described here). Reboot or logout is required to make it working.KDE
Go to "System Settings", select "Power Management" tab in "Hardware" section and then select "Energy Saving" tab, scroll down, find "When power button pressed" dropdown list and change its value to "Do nothing". Click "Apply" button for changes to take an effect.
-
Install xdotool:
sudo apt install xdotool
This one will allow us to trigger key events.
-
Next step is to trigger "End" key press on "Power off" button press.
Ubuntu 18.04
Create
/etc/acpi/events/power
file with the following contents:event=button/power action=/etc/acpi/power.sh "%e"
and create
/etc/acpi/power.sh
script with the following contents:#!/bin/sh xdotool key End
and add execution permissions to it:
sudo chmod +x /etc/acpi/power.sh
Ubuntu 16.04
Backup ACPI script used to handle "Power Off" button's event:
sudo cp /etc/acpi/powerbtn.sh /etc/acpi/powerbtn.sh.backup
Then edit it, find the following:
# If logind is running, it already handles power button presses; desktop # environments put inhibitors to logind if they want to handle the key # themselves.
and modify the code below so it looks like:
if pidof systemd-logind >/dev/null; then xdotool key End exit 0 fi
-
In order to apply these changes one should run:
sudo acpid restart
Thanks to Adam it's possible to restart
acpid
automatically when a user logs in (on system boot). So instead of manualsudo acpid restart
in terminal each time after reboot add the following line:session optional pam_exec.so /usr/sbin/acpid restart
to the end of
/etc/pam.d/common-session
file.
Solution 2
This should do the trick.
Enter in terminal:
gsettings set org.gnome.settings-daemon.plugins.power button-power "nothing"
Related videos on Youtube
Adam Ryczkowski
Updated on September 18, 2022Comments
-
Adam Ryczkowski over 1 year
I've bought the ROG G551JM Asus laptop which proven to be Ubuntu-compatible. The computer's keyboard is ill-designed with the key "Poweroff" being an ordinary key, placed in the spot, where one usually finds the "End" key. The computer is fine otherwise, so I bought it with the assumption, that I would be able to somehow "fix" the problem, i.e. remap the PowerOff button into the "End" key.
The PowerOff button made wrong (ASUS G551JM):
Normally there is an «End» key (ASUS N56VZ):
The "PowerOff" button events are produced by the
/dev/input/event2: Power Button
device (as reported byevtest
) with code 116 (example line fromevtest
:Event: time 1422895638.246142, type 1 (EV_KEY), code 116 (KEY_POWER), value 1
)The key is also visible to X server, and ultimately Ubuntu can identify key presses as a
PowerOff
key and launches the "Goodbye user Adam..." semi-transparent dialog box that confirms my will to actually turn the computer off.The problem is that the binding that binds the PowerOff key press with the routine that displays that confirmation dialog is not visible under dconf-editor and I have no hint where to find it.
Can anyone tell me, where to change (or delete) the default binding for the PowerOff key???
It should be something easy.
edit 21.IX.2016:
There is a way to permanently disable the dialog box, see How do I modify the options for the power button? . I hope that after disabling the power button, it would be possible to re-assign it to something else.
-
ezze over 7 yearsHave you solved the issue? It would be very useful if you post an answer here.
-
Adam Ryczkowski over 7 years@Ezze Not really (I am using different computer because of this issue). But I managed to permanently disablethe shutdown dialog produced after pressing the key. OK, I'll update the answer.
-
ezze over 7 yearsRelated question on Unix & Linux StackExchange.
-
Matthieu over 7 yearsI hate ASUS so much for removing that key.
Home
andEnd
are the "meta" keys I use the most... :(
-
-
Adam Ryczkowski over 9 yearsYES!!! But then there is another thing: how to map it into the «end» key?
-
ezze over 7 yearsUnfortunately, it doesn't help. It disables capturing of "Power off" button as it seen by
xev
command and preventsxmodmap
to remap it. And by the way, "Power off" button's keycode may be different (124 for my ASUS N751JK). -
Johano Fierra over 7 yearsIt should not disable the ability to capture the button... but how about this. Enter in terminal:
gsettings set org.gnome.settings-daemon.plugins.power button-power "shutdown"
(so pressing the button is captured), then edit/etc/acpi/events/powerbtn
and comment out the line#action=/etc/acpi/powerbtn.sh
, and then enter in terminal:xmodmap -e "keycode 124 = End"
This should prevent the power button from doing anything and in the same time map it to the End key. -
Byte Commander over 7 yearsWhat about
sudo acpid reload
instead of restart? Would that work? -
ezze over 7 yearsIt doesn't help too cause "Power off" event is handled not only by
powerbtn.sh
script but also by (at least)systemd-logind
service (probably, it usesbutton-power
setting you mentioned in your answer). You can findif pidof systemd-logind >/dev/null; then
...exit 0
line inpowerbtn.sh
which means that the script letssystemd-logind
to handle "Power off" event by itself when desktop session is active. One thing you are right about — changingbutton-power
value doesn't affect the ability to capture the key but it's already captured by some process (systemd-logind
or other). -
ezze over 7 years
sudo acpid reload
also works for me when I run it manually in terminal. But it doesn't help when I placeacpid reload
in/etc/rc.local
or in bash script specified by LightDM'ssession-setup-script
. -
WinEunuuchs2Unix over 7 years@Ezze Have you tried restarting / reloading acpid in
Startup Applications
? -
Adam Ryczkowski over 7 yearsWhat solves the
acpid restart
problem is to add a linesession optional pam_exec.so /usr/sbin/acpid restart
at the end of/etc/pam.d/common-session
. This way the acpid will get restarted just after user login and this is just enough for me. Problem solved at last!! -
rtindru about 7 years
$ gsettings get org.gnome.settings-daemon.plugins.power button-power 'nothing'
- Even though power is mapped to nothing, my power button still puts the laptop to sleep. What am I missing - Asus UX501V on Ubuntu 17.04 -
ezze almost 6 years@rtindru, you should
gsettings set
instead ofgsettings get
. -
rtindru almost 6 yearsAh, I feel stupid for not seeing this. Thanks @Ezze, works!
-
LGenzelis about 5 yearsIn order to make this work in Kubuntu 18.04, I had to give execution permissions to power.sh:
sudo chmod +x /etc/acpi/power.sh
@Ezze, maybe you could update your answer to include this. -
antichris over 3 years+1. You don't need a
power.sh
script, though. Just put invoke thexdotool
in theaction
, like so:action=xdotool key End