Remap keyboard keys for Ubuntu

In this post, learn how to change the default behavior of keys on the keyboard in Ubuntu, especially useful for multimedia keys.

Newer versions of Ubuntu use xkb for keyboard layouts. xkb works with a hierarchy of multiple files to handle different keyboard settings. New entries can be added to one main config file which can change the behavior of the keys or in other words keys can be remapped.

In this how to I will be changing the top row keys on my Chromebook, but you can use this method for your laptop or desktop keyboard. Wouldn’t it be nice to have multimedia keys on your old keyboard?

Setting up the Chromebook F keys in 3 steps

Update: If you are using this guide for your Chromebook, see this post which will do this automatically for you.

A fresh install of Ubuntu on a chromebook maps the top row keys to the F keys (F1, F2 …). Let’s change them to match their button icons.

On chromebooks there is no caps lock key and in it’s place is a search key. Let’s change that as well.

Step 1: Find keyboad identifiers

Each key has an identifier that needs to be used to tell the system what to do when the key is pressed. For example, the ‘XF86MonBrightnessDown’ tells the key to turn down the screen brightness when pressed.

We can use the xmodmap utility to find these out:

  • Open a terminal window (Ctrl + Alt + T)
  • Type xmodmap -pke
  • You will see a rather long list of all the keyboard identifiers.
  • For convenience let’s make a copy of these identifiers so that we can refer to the contents later.
  • Type xmodmap -pke > ~/keymaptable
  • You will find the new file called keymaptable in your home folder.

Step 2: Add new entries to the main xkb config file

  • In your terminal window, open the xkb file with gedit. Type sudo gedit /usr/share/X11/xkb/symbols/pc
  • You will see various entries in this file that are surrounded by squiggly brackets. This is where we want to add our new entries to override the existing ones.
  • I want my F keys to be remapped like so:

  • Paste these entries into the config file in between the squiggly brackets that define your keyboard (see below).
  • key <FK01> { [XF86Back] };
    key <FK02> { [XF86Forward] };
    key <FK03> { [XF86AudioPlay] };
    key <FK04> { [Print] };
    key <FK05> { [Super_L] };
    key <FK06> { [XF86MonBrightnessDown] };
    key <FK07> { [XF86MonBrightnessUp] };
    key <FK08> { [XF86AudioMute] };
    key <FK09> { [XF86AudioLowerVolume] };
    key <FK10> { [XF86AudioRaiseVolume] };
    key <LWIN> { [Caps_Lock] };
  • Save the file.

Step 3: Clear the xkb settings cache

  • Type sudo rm -rf /var/lib/xkb/* in your terminal.
  • Reboot your computer/laptop.

 

When you log back in you should find that the keys have been remapped, pretty useful huh?

You can of course set the F keys to whatever you want to. Just look in your keymaptable file you created earlier for key identifiers that match your preferred action.

So, what are you waiting for? Make your own customised keyboard!

Links: