Swap some keyboard keys

All we need is an easy explanation of the problem, so here it is.

I would like to swap some keys, like Home and End on my keyboard. I did it using xmodmap, but this gets reverted every time my computer goes to sleep, and some applications, e. g. VSCode don’t respect it.

Is there any way to permanently swap keys and make each app detect it right?

I’m using Ubuntu 20.04.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

Turns out, you can remap keys in the Linux kernel.

  1. Determine the scan code of the key you want to remap to be some other key. This will be a hexadecimal number and you can see it in sudo showkey -s. Be careful, the program will emit both the codes for the keypress event and the release. Let’s say I want to change the behavior of the 0xe021 key (Calculator on my keyboard.)
  2. Determine the key code of the key you want your key to behave as. This will be a decimal number and you can get it with sudo showkey -k. Be careful, the program will emit both the codes for the keypress event and the release. Let’s say I want to have another ‘107’ key (End on my keyboard.)
  3. Issue sudo setkeycodes e021 107, with your codes.
  4. To make it permanent you have to run it after each boot as root. You can solve it with crontab. Run sudo crontab -e and put your command from above at the end of the file (without sudo but with absolute path): /usr/bin/setkeycodes e021 107

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

Leave a Reply