How to change mapping for the «PowerOff» key on keyboard?

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):

  1. 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 use dconf-editor to change org.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.

    Energy saving

  2. Install xdotool:

    sudo apt install xdotool
    

    This one will allow us to trigger key events.

  3. 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
    
  4. 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 manual sudo 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.


This should do the trick.

Enter in terminal:

gsettings set org.gnome.settings-daemon.plugins.power button-power "nothing"