Sunday, October 4, 2015

Simple Key Re-Mapping on Ubuntu 14.04

This tutorial shows how a simple key remapping can be done using XKB, which is the standard keyboard mapper that Ubuntu 14.04 uses.

To give a little background, I have a ThinkPad X201. Around the laptop's arrow keys, there are two keys that act as BACK and FORWARD (as in the Internet-browser sense), which I found really redundant since I always use the combo of "ALT+LEFT" or "ALT+RIGHT" to achieve the same goal. And so I decided to remap these keys to "PG DOWN" and "PG UP" respectively.



As I researched around achieving this, I found that XKB is pretty complex. For example, it allows remapping a key to different keys, depending on the modifiers (e.g. A becomes B, while Alt+A would become Alt+C) While what I wanted was very simple, most of the resources I found online were pages long trying to explain the quirks of XKB. Now that I've learned how to achieve my remapping, I'm doing a brain-dump here :)

Goal: To map "BACK" and "FORWARD" to "Pg Down" and "Pg Up"
Steps:
  1. Figure out the keycodes for "BACK", "FORWARD", "Pg Down", and "Pg Up"
    • The codes can be found at /usr/share/X11/xkb/keycodes/evdev
    • They are: I166, I167, PGUP, and PGDN
  2. Figure out how the keycodes are used
    • Go to /usr/share/X11/xkb/
    • Grep for the keys above:
      • I166 and I167 are used by /usr/share/X11/xkb/symbols/inet, by default mapped to "XF86_Back" and "XF86_Forward"
      • PGUP and PGDN are used by many files. One of them is /usr/share/X11/xkb/symbols/pc, whic shows that they map to "Prior" and "Next"
  3. Customize the keycodes' mapping:
    • In accordance to the finding in (2), change "XF86_Back" and "XF86_Forward" that are used by I166 and I167 to "Prior" and "Next"
  4. Erase XKB cache files
    • Remove all *.xkm files under /var/lib/xkb/
  5. Reboot
  6. Enjoy!
This is a git-change for the above:

aharijanto@aharijanto-ThinkPad-X201:/usr/share/X11/xkb/symbols$ git diff
diff --git a/symbols/inet b/symbols/inet
index 5c4784e..8e522aa 100644
--- a/symbols/inet
+++ b/symbols/inet
@@ -144,8 +144,10 @@ xkb_symbols "evdev" {
     key <I163>   {      [ XF86Mail              ]       };
     key <I164>   {      [ XF86Favorites         ]       };
     key <I165>   {      [ XF86MyComputer        ]       };
-    key <I166>   {      [ XF86Back              ]       };
-    key <I167>   {      [ XF86Forward           ]       };
+//  key <I166>   {      [ XF86Back              ]       };
+    key <I166>   {      [ Prior                 ]       };
+//  key <I167>   {      [ XF86Forward           ]       };
+    key <I167>   {      [ Next                  ]       };
 //  key <I168>   {      [ ]       }; // KEY_CLOSECD (opposite of eject)
     key <I169>   {      [ XF86Eject             ]       };
     key <I170>   {      [ XF86Eject, XF86Eject  ]       };


1 comment:

  1. Thank you for this concise write-up! I'd done this remapping a while back, but it either got overwritten, or was never done on this particular laptop.

    I have a ThinkPad T410, and this would drive me crazy. I like working on crossword puzzles, but sometimes when navigating via the arrow keys, I'd hit the Page Back, and *poof*, my puzzle is gone. So this fix is great to have.

    ReplyDelete