UP | HOME

Date: [2020-05-31 Sun]

Key Code

Table of Contents

When we press a key on the keyboard an `scan code' is emitted from the keyboard to the system.

The kernel reads this scan code and tries to find it in a table that maps `scan codes' to `key code'

In most cases, we can assign to this scan code the key code of the functionality that we want, as the X system already support a wide variety of keys and most desktop environments interpret this keys and give them that very same functionality within them.

1. Obtaining the scan code of unrecognized keys

When the kernel can't find the scan code of a pressed key in the map of key codes, the kernel produces an error saying that the key is unknown, and indicate us the scan code of the key in question.

You can press the unrecognized keys and use dmesg to find the scan codes, you would see something similar to this but with your own scan codes:

atkbd.c: Unknown key pressed (translated set 2, code 0xa6 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e026 <keycode>' to make it known.
atkbd.c: Unknown key released (translated set 2, code 0xa6 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e026 <keycode>' to make it known.
atkbd.c: Unknown key pressed (translated set 2, code 0xa3 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e023 <keycode>' to make it known.
atkbd.c: Unknown key released (translated set 2, code 0xa3 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e023 <keycode>' to make it known.
atkbd.c: Unknown key pressed (translated set 2, code 0x94 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e014 <keycode>' to make it known.
atkbd.c: Unknown key released (translated set 2, code 0x94 on isa0060/serio0).
atkbd.c: Use 'setkeycodes e014 <keycode>' to make it known.

The other option is to use tail -f in the log file of the kernel, again, it may be in /var/log/messages or in /var/log/dmesg, and then press your unrecognized keys, to exit tail use ctrl+c: tail -f /var/log/messages

By this method you would see the same messages appear when you press said keys, only with the date and the user name in front.

2. Assing a key code to the scan code

< Collapse code block> Expand code block
xmodmap -pke | less

ADD 8

This command will show you the current layout of the keyboard, in here you can both find unused key codes and well as the key codes of functionality already supported by xmodmap. If this three of the keys corresponded to the volume controls, mute, lower the volume and increase the volume, we will find that this functionality already have a key code associated to it, we will see the following lines:

keycode 121 = XF86AudioMute NoSymbol XF86AudioMute
keycode 122 = XF86AudioLowerVolume NoSymbol XF86AudioLowerVolume
keycode 123 = XF86AudioRaiseVolume NoSymbol XF86AudioRaiseVolume

So, we are going to assign each of the scan codes one of the key codes that we found here. To assign a key code to a key, we use the command setkeycodes. Following our example:

setkeycodes e014 121 setkeycodes e023 122 setkeycodes e026 123

If we don't get error messages then the keys has been correctly set. This should make the keys work. It is a good idea to try the keys individually to check that everything is working well. The worst case scenario is that we assign an invalid number and our keyboard stops working correctly, but this can be easily undone by reloading the session (logging out and logging in) or restarting. This shouldn't happen tho. Make a script that runs at boot time

Once all is nicely working, we are going to create a script to do this assignation when we boot the computer. We create a file inside /etc/init.d (in some systems it may be in /etc/conf.d, for Ubuntu and variants it's /etc/init.d), I am going to call it keyremap. sudo gedit /etc/init.d/keyremap

And then add the following lines in this file: #!/bin/sh setkeycodes e014 121 setkeycodes e023 122 setkeycodes e026 123

We save the file, and then we make it executable with: sudo chmod +x /etc/init.d/keyremap

And make it be executed at the start together with the rest of the scripts in /etc/init.d: update-rc.d keyremap defaults

If we wanted to remove the script from the start, we can use: update-rc.d -f keyremap remove


Backlinks


You can send your feedback, queries here