Saturday, 13 June 2015

Remapping keys in Linux using Xmodmap

In the last post, I showed you how to remap keys in linux using Autokey but the problem with that was it didn't allow us to remap modifier keys such as Ctrl, Shift keys etc. In this tutorial I am going to show you how to remap any key on the keyboard using two tools, xmodmap and xev.

Each key in the keyboard is represented with a keycode, which is a fixed number and has a keysym assigned to it, which is the action or the behaviour of that key. The keycode is fixed for a key and cannot be changed but the action or keysym of the key can be changed. For example, the right Ctrl key in the keyboard, has a keycode of 105 and keysym Control_R. In general if I want one key to do something else, I just need to change its keysym.
If I want to remap key K1 to another key K2, I need to know
                    1. The keycode of key K1.
                    2. The behaviour or keysym of key K2.
The keycode and the behaviour of a key can be found out using a tool called xev.

Finding the Keycode and Keysym of a key:
1. Open the terminal and type xev.
2. Press the key for which keycode and keysym has to be found.
3. Skip the first 115 lines or so until you to get to "KeyRelease event, serial......"
    The third line from here is what is important for us.
For example, for the g key, the third line looks like this:
state 0x0, keycode 42 (keysym 0x67, g), same_screen YES, So the g key has a keycode 42 and keysym g.

Remapping keys:
Let's say I want to remap g key to the h key(Pressing g will have the effect of pressing h.)
I've already found out the keycode and behaviour of g in the previous step.
I found that the keycode and behaviour for the h key is  43 and h.
When I say, I want to remap g key with h, I mean that I want to change the behaviour of the key with keycode 42 which is actually g to h.
Open the terminal and type
xmodmap -e "keycode 42 = h" The general command is
xmodmap -e "KEYCODE = B1 B2 B3 B4 B5 B6" Here B1 to B6 represents the behaviour when the key is pressed in the following combination:
1. key
2. Shift + key
3. mode_switch + key
4. mode_switch + Shift + key
5. AltGr + key
6. AltGr + Shift + key

For exmaple if I want "h" when I press g and "!" when I press Shift + g, I will have to give the command
xmodmap -e "keycode 42 = h exclam" Here "exclam" is the behaviour(keysum) of "!".
A list of keysyms can be found here.

Modifiers:
There are 6 modifiers in xmodmap. A list of them can be got by typing xmodmap into the terminal. This is what I got:
xmodmap:  up to 4 keys per modifier, (keycodes in parentheses):

shift       Shift_L (0x32),  Shift_R (0x3e)
lock        Caps_Lock (0x42)
control     Control_L (0x25),  Control_R (0x69)
mod1        Alt_L (0x40),  Alt_R (0x6c),  Meta_L (0xcd)
mod2        Num_Lock (0x4d)
mod3     
mod4        Super_L (0x85),  Super_R (0x86),  Super_L (0xce),  Hyper_L (0xcf)
mod5        ISO_Level3_Shift (0x5c),  Mode_switch (0xcb)
Remapping modifier keys:
Remapping between modifier keys need some extra steps.
For example to swap the right Ctrl and right Shift keys, execute the following commands:

1. Clear the appropriate modifier.
xmodmap -e "clear control"
xmodmap -e "clear shift"
2. Remap the keys.
xmodmap -e "keycode 105 = Shift_R"
xmodmap -e "keycode 62 = Control_R"
3. Add the cleared keybindings.
xmodmap -e "add shift = Shift_L Shift_R"
xmodmap -e "add control = Control_L Control_R"
If I wanted to remap Caps lock with something else, I would do clear lock, do the remapping and then add lock = Caps_Lock.

Make the changes permanent:
The changes we made will be active only for this session. We will loose the changes once we log out. To preserve the changes,

1. Create a file called ".Xmodmap" in the /user/<user> folder.
2. Type down only the command within the quotes.
    For the example above it would be:
clear control
clear shift
keycode 105 = Shift_R
keycode 62 = Control_R
add shift = Shift_L Shift_R
add control = Control_L Control_R
3. Save and close.

Now create a file called ".xinitrc" in the same folder and type "xmodmap .Xmodmap" save and close.


No comments:

Post a Comment