Remap/change your secondary/usb keyboard keys

I normally work on laptop with a secondary usb keyboard. My fingers are used to laptop keyboard but when it comes to usb keyboard they some time has different keys layout so its hard to find home/end/pageup page down buttons and here starts the frustration.

We have several ways in linux to swap keys or remap keys of keyboard but there is no way to change it for the secondary or usb keyboard, but luckily we have an application that can change or remap your keyboard keys for multiple keyboards, that is “xkbcomp”.

You can use “xkbcomp” to remap keys of your external keyboards.

before using “xkbcomp” you must know the device id of your secondary keyboard.

Run following command to list your input devices

$ xinput

and look for

USB USB Keyboard                          id=15   [slave  keyboard (3)]

Note the device id “15”

Now export current layout of your keyboard into a .xkb file, note the “-i 15”
“-i” tells xkbcomp to look for a device id and “15” is the id of your device.

$ xkbcomp -i 15 $DISPLAY ~/myusbkblayout.xkb

Edit the file generated by xkbcomp

$ nano ~/myusbkblayout.xkb

Change key codes ans save the file

For example my home and insert keys maps are as follow in output file

    <HOME>= 110;
      <UP>= 111;
    <PGUP>= 112;
    <LEFT>= 113;
    <RGHT>= 114;
     <END>= 115;
    <DOWN>= 116;
    <PGDN>= 117;
     <INS>= 118;

and I want to swap Home and Insert buttons then I will just change codes against map like below

     <INS>= 110;
      <UP>= 111;
    <PGUP>= 112;
    <LEFT>= 113;
    <RGHT>= 114;
     <END>= 115;
    <DOWN>= 116;
    <PGDN>= 117;
    <HOME>= 118;

Now we are going to upload keyboard layout file back to linux so it can adopt it

$ xkbcomp -i 15 ~/myusbkblayout.xkb $DISPLAY

Your second keyboard should be working as you want in few seconds.

For more info please look at official arch linux wiki

3 thoughts on “Remap/change your secondary/usb keyboard keys

  1. Very cool. But obviously if you disconnect your device and reconnect it the map is gone and has to be loaded in again, and it is entirely possible for your device to show up under a different id this time.

    If you’re in luck and your composite device has a unique name for its pointer and its keyboard then you can use that instead of its id in your call to xkbcomp. A slight improvement but still doesn’t solve the thing entirely.

    You can get the ID from the name even if it is the same for the pointer/keyboard by doing some more trickery:

    e.g. consider that you have a composite device which is a mouse with additional buttons which it lists as a keyboard.

    xinput shows this:

    ⎡ Virtual core pointer id=2 [master pointer (3)]
    ⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
    ⎜ ↳ USB Laser Game Mouse Consumer Control id=17 [slave pointer (2)]
    ⎜ ↳ USB Laser Game Mouse id=19 [slave pointer (2)]
    ⎣ Virtual core keyboard id=3 [master keyboard (2)]
    ↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]

    ↳ AT Translated Set 2 keyboard id=15 [slave keyboard (3)]
    ↳ USB Laser Game Mouse id=16 [slave keyboard (3)]
    ↳ USB Laser Game Mouse Consumer Control id=18 [slave keyboard (3)]

    The keyboard we’re after is id=16. So we can ask xinput for the id:

    xinput –list –id-only “keyboard:USB Laser Game Mouse”

    Combined with the xkxcomp stuff:

    xkbcomp -i $(xinput –list –id-only “keyboard:USB Laser Game Mouse”) ~/myusbkblayout.xkb $DISPLAY

Leave a Reply

Your email address will not be published. Required fields are marked *