How to disable multimedia keys entirely? 18.04 LTS

Are keyboard multimedia keys different?

Yes, these keys are a little different. They might report a single key code or multiple key codes when pressed with other keys like Fn for example. Furthermore, it is oftentimes difficult to identify their key codes with utilities like xev. This depends on the keyboard's manufacture's configuration.

Multimedia keys, usually, have their key codes configured with XF86 key names. These names differ but start with XF86 like XF86MonBrightnessUp and XF86MonBrightnessDown and so on.


How to disable/reconfigure keyboard multimedia keys?

XKB Method

The easiest method is to edit the XKB configuration file like so:

  1. Edit the XKB /usr/share/X11/xkb/symbols/pc file by running the following command in the terminal:

    sudo nano /usr/share/X11/xkb/symbols/pc

  2. Find lines that contain XF86 like so:

    key <KEY_CODE> { [ XF86MonBrightnessDown ] };

  3. Comment them out by adding // before them like so:

    //key <KEY_CODE> { [ XF86MonBrightnessDown ] };

  4. Save the file and exit the editor by pressing Ctrl + X then press Y.

  5. Clear the XKB cache by running the following command in the terminal:

    sudo rm -rf /var/lib/xkb/*

  6. Reboot your system to activate your new XKB configuration or alternatively you can avoid reboot and try to reload the new XKB configuration by setting an XKB map layout using the following command in the terminal:

    setxkbmap -layout us

  7. Test your keys.

If the keyboard multimedia keys are configured by the manufacturer to report a single key code, the above solution might disable them and leave the keys free to be used for other purposes.

If, however, this is not the case, then you will need to deal with xmodmap.

xmodmap Method

The xmodmap method is a little different and you cannot just disable multimedia keys using this method and assign them to nothing like so xmodmap -e 'keycode Number =' because this, oftentimes, will render them unusable so you will need to reassign them to the desired functions. You will also need to put in action some mechanism to maintain your changes between reboots and logouts/logins.

To implement this method, please follow the steps below:

  1. Show the current keyboard map by running the following command in the terminal:

    xmodmap -pke

  2. Inspect the output and identify lines that contain XF68 right after the = sign like so:

keycode 232 = XF86MonBrightnessDown NoSymbol XF86MonBrightnessDown NoSymbol XF86MonBrightnessDown

The above line, for example, shows that the key with code number 232 is currently configured to trigger XF86MonBrightnessDown which will decrease the monitor's brightness and you can change this behavior by assigning a different value to it like so xmodmap -e 'keycode 232 = New_Value'. For example to assign the F11 functionality to this multimedia key, please run the following command in the terminal:

xmodmap -e 'keycode 232 = F11'

The new functionality will be effective immediately. This change in functionality will, however, be lost after reboot or logout/login.

Notice: It might be helpful to run the acpi_listen command in the terminal and monitor the output as you press the actual physical key then compare it to the output you got from xmodmap -pke so that you confirm the key code is for the one you want. You can also try the xev utility but, unfortunately it will not always return a key code when dealing with multimedia or vendor specific keys.

To preserve the change after reboots and logouts/logins, you will need to do the following:

  1. Create and edit a script file in your home directory by running the following command in the terminal:

    nano ~/.Modify_Multimedia_Keys.sh

  2. Add this #!/bin/bash in the first line then add your xmodmap -e 'keycode Number = New_Value' commands below the first line ( each command in a single new line ) like so:

#!/bin/bash
xmodmap -e 'keycode 232 = F11'
xmodmap -e 'keycode 122 = F2'
  1. Save the script file and exit the editor by pressing Ctrl + X then press Y.

  2. Make the script file executable by running the following command in the terminal:

    chmod +x ~/.Modify_Multimedia_Keys.sh

  3. Make the script file execute at each start-up either by adding it to your Startup Applications through the GUI or by placing a Modify_Multimedia_Keys.desktop file in the ~/.config/autostart/ directory that contains the following content replacing YOUR_USERNAME with your actual username:

[Desktop Entry]
Type=Application
Exec=/home/YOUR_USERNAME/.Modify_Multimedia_Keys.sh
Hidden=false
X-GNOME-Autostart-enabled=true
Name=Modify Multimedia Keys
Comment=This modifies keyboard multimedia keys

For posterity, here is the shell script with the xmodmap commands needed to map keycodes to keys for this situation:

#!/bin/bash

# fix the common F keys
xmodmap -e 'keycode 128 = F3'
xmodmap -e 'keycode 212 = F4'
xmodmap -e 'keycode 237 = F5'
xmodmap -e 'keycode 238 = F6'
xmodmap -e 'keycode 173 = F7'
xmodmap -e 'keycode 172 = F8'
xmodmap -e 'keycode 171 = F9'
xmodmap -e 'keycode 121 = F10'
xmodmap -e 'keycode 122 = F11'
xmodmap -e 'keycode 123 = F12'

# fix the '=' on the numeric keypad
xmodmap -e 'keycode 125 = equal' 

I determined the keycodes using xev as described in this answer. Note that the Apple Aluminum keyboard has F keys all the way to F19 (!), which are not covered in the script above because I don't really use them. It ought to be possible to use xev to determine what the keycodes for those keys are and add them to the script (if needed).