Remap Keyboard Keys in Ubuntu 24.04 without 3rd Party Apps

Last updated: September 3, 2024 — 1 Comment

This is a step by step tutorial shows how to manually remap (or disable) keyboard keys in Ubuntu without using any third-party apps.

There are a few free open-source applications that can re-map your mouse, keyboard, and other input devices keys in Linux. As far as know, they include input remmaper and AntiMicroX.

For GNOME, the default desktop for Ubuntu and Fedora Workstation, there’s popular GNOME Tweak tool (available in AppCenter/GNOME Software) with options to remap Ctrl, Alt, and Caps Lock keys.

GNOME Tweaks

Without the tools mentioned above, Linux also provides system config files to manually re-map your keyboard key-codes. And, here I’m going to show you how to do the trick in Ubuntu (tested in 24.04).

NOTE: The method in this tutorial can be only used for simple 1:1 key remaps.

Step 1: Find out the scancodes of the keys you want to remap

When you pressing a physical key on keyboard, it sends a scancode to the computer. Then, your system maps the scancode to a virtual keycode. By re-mapping the scancode to another keycode as you want will do the job.

1. To find out the scancode of the keys, first, press Ctrl+Alt+T on keyboard to open up a terminal window. When it opens, run command:

evtest

If command not found, run sudo apt install evtest to install. And in the terminal output, it will print all the available input devices.

In my case, I have laptop’s built-in keyboard (AT Translated Set 2 keyboard) and wireless keyboard (YICHIP Wireless Device) using a USB receiver.

As the screenshot below shows, I have to type 7, the number in /dev/input/event7, for the wireless keyboard.

2. Once you input the corresponding number and hit enter in last command, you can then hit any key on keyboard. The terminal will output the information about what you pressed/released.

In the screenshot below, the value code (e.g., 70028, 70025, 700e0, 700e2) are the scancodes for the corresponding key press/release.

NOTE: Different keyboards have different scan-codes for even same key-codes. In my case, the built-in laptop keyboard includes 1d, 1e, 1f, 20, etc scancodes, while the wireless keyboard has 700e0, 700e2, 70025, and so forth.

Step 2: Get the ID of your Keyboard

Before remapping, you also need to find out the information to identify your keyboard.

For USB keyboard, simply run the command below in terminal (Ctrl+Alt+T):

lsusb

In the output, find out your keyboard according to its name. And write down the ID. It’s usually xxxx:xxxx, while the first 4 numbers is vendor ID and later 4 is product ID.

Also, run the command below to tell the bus ID (usually 03 in hex):

cat /usr/include/linux/input.h |grep BUS_*

If it’s NOT USB device, try running the command below instead to get the modalias:

cat /sys/class/input/event7/device/modalias

In the command, replace event7 for the correct device according to the evtest command in “Step 1”. In terminal output, copy the “input:b****v****p****e****”

Step 3: Add udev rule to remap keyboard key

After found out the scancodes of the keys you want to remap, and the ID for your keyboard device, you can then edit the udev config file to remap.

First, in Ubuntu Linux there’s built-in /usr/lib/udev/hwdb.d/60-keyboard.hwdb config file for scancode and keycode mapping.

However, it’s recommended to create a rule under /etc/ directory for user custom key mapping.

1. First, run command in terminal (Ctrl+Alt+T) to create and edit the udev config file:

sudo gnome-text-editor /etc/udev/hwdb.d/61-keyboard-local.hwdb

Here, replace gnome-text-editor with gedit for 22.04, or mousepad for XFCE, pluma for MATE, or nano command line text editor that works in most desktop environments.

2. When files opens, add similar lines below:

evdev:input:b0003v3151p3020e0200*
 KEYBOARD_KEY_7001e=reserved
 KEYBOARD_KEY_700e2=leftctrl
 KEYBOARD_KEY_700e0=leftalt

The first line evdev:input:b0003v3151p3020e0200* is used to identify which keyboard device to remap. While input:b0003v3151p3020e0200 is the modalias you got in “Step 2”.

In my case, it can be replaced with evdev:input:b0003v3151p3020*. While, 0003 is the USB bus ID, 3151 is vendor ID, and 3020 is product ID. And, both has * in the end.

And following lines tell to remap the scan-codes to your desired key-codes:

  • KEYBOARD_KEY_7001e=reserved – tells to disable the key whose scancode is “7001e”.
  • KEYBOARD_KEY_700e2=leftctrl – tells to remap the key whose scancode is “700e2” (Left Alt in my case) to Left Ctrl.
  • KEYBOARD_KEY_700e0=leftalt – remap the key whose scancode is “700e0” (Left Ctrl in my case) to Left Alt.

The 3rd line tells to remap the key (left alt in my case) whose scancode is “700e2” to left ctrl. While the last line remap scancode 700e0 to left alt.

For choice, you may replace keycodes (lowercase) by adding ‘key_’ at beginning, so it will be ‘key_leftctrl’, ‘key_leftalt’, ‘key_1’, ‘key_2’, ‘key_a’, ‘key_b’, ‘key_comma’, ‘key_dot’, or something similar (evtest command output can tell)…

You can add as many lines as you want with KEYBOARD_KEY_scancode=keycode layout. While, each line has a blank space at beginning.

After editing, save file. For nano text editor, press Ctrl+S to save, then Ctrl+X to exit.

Step 4: Apply changes

After editing udev rule to remap keyboard keys, you can run the commands below one by one to apply:

  • First, update the hardware database index:
    sudo systemd-hwdb update
  • Next, reload the new key mappings:
    sudo udevadm trigger

NOTE: The commands will NOT work for key remaps you later deleted from the udev rule. As workaround, either reboot or add/change rule to remap to original keycode.

Finally, try to either hit the keys on keyboard to see the changes, or use the command below

udevadm info /dev/input/by-id/* | grep KEYBOARD_KEY

The remapped keys will persistent after reboot, because the systemd-hwdb-update.service includes a trigger ConditionNeedsUpdate=/etc that automatically update hardware database index, and kernel auto-reload it on system boot.

Twitter

I'm a freelance blogger who started using Ubuntu in 2007 and wishes to share my experiences and some useful tips with Ubuntu beginners and lovers. Please comment to let me know if the tutorial is outdated! And, notify me if you find any typo/grammar/language mistakes. English is not my native language. Contact me via [email protected] Buy me a coffee: https://ko-fi.com/ubuntuhandbook1 |

One response to Remap Keyboard Keys in Ubuntu 24.04 without 3rd Party Apps

  1. A much easier remapping alternative is to edit the keyboard map in text editor. This is not for the faint-hearted because if you make an error you may be left without an operable keyboard and will need to recover using the screen keyboard. You can navigate the process below in safety using ‘files’ and plan what you are going to do without being able to save it.

    1. Open a terminal – Ctl-Alt-T
    2. Open the nautilus file manager as administrator – sudo nautilus admin:/ – You will get a warning, but it will load after a few seconds. Remember to keep the terminal open.
    3. Navigate to the code for your keyboard layout – other locations/Ubuntu/usr/share/X11/xkb/symbols – and find your keyboard layout from the list.
    4. Make a duplicate of this page for safety purposes.
    5. Open the page.
    6. Look for the details of the key(s) you wish to remap.
    7. Edit them accordingly. Make sure you do this accurately. Each line must be in the form – key { [ a, A, aacute, Aacute ] }; – the position of the brackets, commas and semicolon are important
    8. If you don’t know the precise name for the character you wish to use you may be able to find it by looking at other layouts. Alternatively you can use UTF8 definitions – e.g. U266D is the musical flat sign.
    9. When you are completely satisfied with the new layout open the drop-down menu and click ‘Save’.
    10. Close the Terminal.
    11. Reboot the computer and your new layout will be working.

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*