How do I disable the sleep button on my keyboard?

I have a "power button" on my keyboard that enables standby or hibernate. I can't find the mapping of this key anywhere in "sytem settings>Keyboard", "system>power management", "compiz config> or similar (where I could find it before unity). How can I disable (not remap) this button?

I do not want to disable hibernate/sleep altogether, it's simply this key that gets pressed a lot by accident and it's getting quite annoying.


Solution 1:

Disable the suspend button in your system

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

Revert to the default value

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

Disable the sleep button in your system

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

Revert to the default value

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

Disable the hibernate button in your system

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

Revert to the default value

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

Settings those options above does not disable the functions in your system, you can still use the cog wheel menu to hibernate / suspend, this only disables keyboard and other buttons you might have in your computer.

Solution 2:

Dconf Editor is the recommend replacment to gconf editor for Unity. You can get dconf in the software center or by typing sudo apt-get install dconf-tools in the terminal.

With Dconf editor, navigate to org gnome settings-daemon plugins power. There you can change what the sleep button does, as well as many other buttons on your keyboard.

enter image description here

Solution 3:

Since Ubuntu 18.04, Bruno Pereira's answer doesn't seem to work anymore.

To disable any key, you will need to get its code:

xmodmap -pk | grep -i sleep

For me, the sleep button has the code 150 as you can see:

    150     0x1008ff2f (XF86Sleep)  0x0000 (NoSymbol)   0x1008ff2f (XF86Sleep)  0x0000 (NoSymbol)   0x1008ff2f (XF86Sleep)  0x1008ff2f (XF86Sleep)

Once you have the key code, you mainly have two ways to automatically disable it.

Disable for some users

If you just need to disable it for one or many users, you can add this command to the Startup Applications, assuming the key code of your sleep button is 150:

xmodmap -e 'keycode 150='

If the user logs off, the button will be enabled again. If you want to keep it disabled, try the other method instead.

Disable for every users

The button will be disabled after each booting. You will need the administrator privileges for this. Edit this file:

sudo gedit /usr/share/X11/xkb/keycodes/evdev

The line we will have to edit looks like <SOMETHING> = <your_keycode>. Once you found it, add // at the beginning to comment it. In my case, I had to change this:

    <I150> = 150;   // #define KEY_SLEEP               142

Into this:

//  <I150> = 150;   // #define KEY_SLEEP               142

Note: With both methods, the button will still be enabled for few seconds when booting or logging in.

Sources:

  • How to disable a keyboard key in Linux (Ubuntu)?
  • How can I persistently remap keys in Ubuntu 16.04?