Fascinating Captain

Tech Guides and Buying Advice

howto

Remap keyboard keys for Ubuntu

Make the keys on your laptop bend to your will!

Make the keys on your laptop bend to your will!

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

Ubuntu has a built in way of changing the shortcuts that are mapped to keys on the keyboard. You can find it in the System Settings under Keyboard in the Shortcuts tab. The problem is that you can’t set things like the Super and Caps Lock keys.

Ubuntu has a built in way of changing the shortcuts that are mapped to keys on the keyboard. You can find it in the System Settings under Keyboard in the Shortcuts tab. The problem is that you can’t set things like the Super and Caps Lock 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.

toprowkeys

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

chromebooksearchposition

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:

toshibachromebook2keyboardlayout

  • Paste these entries into the config file in between the squiggly brackets that define your keyboard (see below).
  • Save the file.
Add these entries to the xkb config file

Add these entries to the xkb config file:
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] };

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:

30 Comments

  1. Jan Eisen

    Works like a charm!

  2. Salil

    Hi, your website is brilliant. Got ubuntu working on the chromebook in one evening. Left a 50 euro donation to John Lewis. I have been trying to get this done since June, when I bough the chromebook. Can you help with one more thing – skype! The damn thing does not work, tried quite a few recommended fixes.

    • Captain

      I’m glad you like the site, words of encouragement are always welcome 🙂 Well done for donating to John, I think he more than deserves it. As for Skype, you need to enable the Canonical Partners repository. You can do this by going to Software and Updates > Other Software and ticking the boxes for Canonical Partners. You will then be able to install Skype from Software Centre. I haven’t been able to find a fix for the mic though so you’ll have to try an external mic.

  3. John

    I followed these exact instructions. Everything appears to be working except for the F6 key which is supposed to lower the brightness. I assume others have not experienced this issue?

    • Captain

      That’s strange, I’ve just checked and F6 for brightness still works for me. Maybe double check for any typos. Also check xmodmap -pke to verify the brightness up identifier.

    • Jacob

      I am having a same experience with Ubuntu Mate 16.04.
      When I tried this is Elementary OS, it worked fine.

      • Captain

        I have not tried Ubuntu Mate, but can confirm that sound and touchpad work if I do a clean install and then not run the system updates.

  4. reez

    This is the closest I can find to what Id like to do although I have keys on my keyboard that arent being used and Id like to assign then to something for example Id like to use an unused key next to the spacebar as a right super key and so on. Question is, with the above table and config file, I have blank keycodes arent clear to map them on my keyboard and in the xkb file i dont seem to find those blank keys there at all. This seems to be the way to go about swapping keys around and modifying keys that are already being detected and have functions, i want to add keys that arent being detected and have no functions (adding functions like duplicating another keys function to these unassigned keys).
    Anyone able to help? It will make this a ton easier.

  5. Willem de Groot

    Thanks a lot! Any suggestion on how to access the original Function keys (so get F10 with the “search”+”volumeup” button)?

    • Captain

      Hi Willem, I think you would use the modifier map definitions but I haven’t worked out how yet!

  6. M

    Do you know how to switch Alt Left and Ctrl Left?

    • Captain

      You can just add LALT and RCTL to the xkb definitions as described in this post and match them to what you want them to do.

  7. M

    unfortunately, that won’t work, since higher up in that file it includes altwin function… I had to add/change the control and alt functionality in there. It works, except for Alt+Tab to switch windows unfortunately. Which I use often…

  8. joseph

    hi sir, may i ask how do you come up with keyword FK01 and so on to FK10 ? can you please share the available keywords please.

    thank you very much.

    • Captain

      Hi Joseph,
      If you look in /usr/share/X11/xkb/keycodes/xfree86 you will see all the key definitions like AE01, BKSP, AB05 and so on. Some of the names are obvious like BKSP for the backspace key. Others like AE01 might not be.

      All alphanumeric characters start with A and the next letter represents the row that the key is in. The bottom row on the keyboard (the row with the spacebar) is row A, the next row up is row B and so on.

      So for AE01:
      (AE01) – alphanumeric key
      (AE01) – in row E
      (AE01) – is the first key in that row

      That makes AE01 the Q on most keyboard layouts.

      Hopefully that makes sense.

      • joseph

        thank you very much for replying, and that was very quick too.

        i am new to linux and my keyboard need to use 2 fingers to use the “home” and “end” key which i hate very much. thats the reason i’m here.

        thank you very much again for your help.

        • Captain

          Ok, you need to pick a key to use for home and end so if you chose F1 and F2, you would use:

          key (FK01) { [ Home ] };
          key (FK02) { [ End ] };

          (replace the brackets with the arrow brackets, it appears that they are getting deleted by the commenting system.

  9. joseph

    hi sir, could you please advice me why the following does’nt work ..

    key { [Home] };
    key { [End] };

    thank you very much.

    • joseph

      i refer to my previous posting.

      i dont know why this system deleted my “greater than” and “lower than” signs and the text between them. this made my previous posting read so stupid.

      what i actually did earlier was i assigned the pause key to “home” and the insert key to “end”, and it didnt work.

      the syntax i used was exactly as told.

      please advice

      thank you very much.

      • Captain

        You’re right the commenting system I’m using is removing the ‘greater than’ and ‘lower than’ signs and the text between them, sorry!.
        If it’s not working it’s maybe you forgot to run the command sudo rm -rf /var/lib/xkb/* or your pause and insert keys are modifier map definitions that I haven’t been able to figure out.
        Maybe you could use other keys instead of the pause and insert keys?

  10. Kurt

    Hey, thanks for putting this out here, but in my case I rebooted and my keyboard doesn’t work any longer. I’m guessing this means I’m hosed and have to redo everything starting with the install, but I’m wondering what could have caused that. Does it interact badly with encrypted drives, or did I put it in the wrong spot within the file? Sorry, very inexperienced user here, just wanting to get a delete key and other stuff running on my ubuntu-C720. Thanks!

    • Captain

      Hi Kurt,

      It sounds like you have an incorrect setting in the keyboard config file which is why the keyboard doesn’t work anymore. I’ve done this myself when I was testing and sorted it out using chroot. If you’re inexperienced you’re better of doing a fresh install of Linux and running my script that will make the keys work for you.

      • Kurt

        Thanks very much! In the end, what was driving this for me was a desire to have a delete key on my C720. I’ll definitely run that script to get all the functionality in the top row keys working and get the F keys back, but do you know of a way I could get a delete key by something like Shift+Backspace or something? Thanks again for making things like this script available!

        • Captain

          Do the following in a terminal window:

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

          On the line near the top of the file (about the 10th line) you’ll see the BKSP definition as Backspace, Backspace. Change that to Backsace, Delete. Save the file and run the following command:

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

          After a reboot, SHIFT + Backspace will work as the delete key.

  11. Daniel Añez Scott

    Hi, I have a Chromebook with GalliumOS and though there’s a universal fullscreen key (F4), I’d like to turn F5 (the key with the square and ||) into F11, to full screen on browsers. Could you help with this?

    • Captain

      Hi Daniel,

      I’ve had a look and can’t find the xbd symbol for full screen, otherwise you would be able to just assign that to the F5 key using the method described in this post. Sorry.

  12. Yek

    The following approach is simple and easy. It’s what I use running GalliumOS 2.0 on an Acer C720.

    1. Open a Terminal
    2. Type”xev”
    3. Press enter
    4. Press various keys on your keyboard
    5. Notice the keycodes that appear in the terminal
    6. Navigate to your home directory
    7. Create a text file named “.Xmodmap”
    8. Open “.Xmodmap”
    9. Map keycodes to commands
    10. Logout or restart your computer
    11. Smile!

    Below are some keycodes I mapped:

    ! ~/.Xmodmap

    keycode 51 = emdash endash
    keycode 67 = Home
    keycode 68 = End
    keycode 69 = Page_Up
    keycode 70 = F11
    keycode 71 = Page_Down
    keycode 124 = Delete
    keycode 121 = XF86AudioMute
    keycode 122 = XF86AudioLowerVolume
    keycode 123 = XF86AudioRaiseVolume
    keycode 72 = XF86MonBrightnessDown
    keycode 73 = XF86MonBrightnessUp

    The above has one little glitch:

    “When NumLock is on, brightness keys and power button don’t work. xev reports different keypresses for these keys with NumLock on or off… ” Source: https://github.com/GalliumOS/galliumos-distro/issues/197

    To workaround this glitch I ensure NumLock on my external keyboard is off when I want to change the brightness settings.

    • Captain

      Hi Yek,

      GalliumOS already maps the keys for you. The method of using a .Xmodmap config file doesn’t work in all distros in my initial testing anyway (I wrote this post way back in Oct 2015).

      As with all things Linux there are many different ways to get things working and I’m sure this will help some readers. Thanks for posting!

Leave a Reply

Product names, logos, brands, and other trademarks are the property of their respective trademark holders.