{"id":47158,"date":"2024-09-03T09:33:18","date_gmt":"2024-09-03T09:33:18","guid":{"rendered":"https:\/\/ubuntuhandbook.org\/?p=47158"},"modified":"2024-09-03T09:33:18","modified_gmt":"2024-09-03T09:33:18","slug":"remap-keyboard-keys-ubuntu","status":"publish","type":"post","link":"https:\/\/ubuntuhandbook.org\/index.php\/2024\/09\/remap-keyboard-keys-ubuntu\/","title":{"rendered":"Remap Keyboard Keys in Ubuntu 24.04 without 3rd Party Apps"},"content":{"rendered":"<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/05\/keyboard-icon.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-thumbnail wp-image-40367\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/05\/keyboard-icon-250x250.webp\" alt=\"\" width=\"250\" height=\"250\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/05\/keyboard-icon-250x250.webp 250w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/05\/keyboard-icon-300x300.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/05\/keyboard-icon-600x600.webp 600w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/05\/keyboard-icon-768x768.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2022\/05\/keyboard-icon.webp 1200w\" sizes=\"auto, (max-width: 250px) 100vw, 250px\" \/><\/a><\/p>\n<p>This is a step by step tutorial shows how to manually remap (or disable) keyboard keys in Ubuntu without using any third-party apps.<\/p>\n<p>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 <a href=\"https:\/\/ubuntuhandbook.org\/index.php\/2021\/07\/remap-keyboard-gamepad-ubuntu\/\" target=\"_blank\" rel=\"noopener\">input remmaper<\/a> and <a href=\"https:\/\/ubuntuhandbook.org\/index.php\/2020\/10\/antimicrox-map-mouse-keyboard-gamepad\/\" target=\"_blank\" rel=\"noopener\">AntiMicroX<\/a>.<\/p>\n<p>For GNOME, the default desktop for Ubuntu and Fedora Workstation, there&#8217;s popular GNOME Tweak tool (available in AppCenter\/GNOME Software) with options to remap Ctrl, Alt, and Caps Lock keys.<\/p>\n<div id=\"attachment_47159\" style=\"width: 620px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/gnome-tweaks-remap.webp\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-47159\" class=\"size-large wp-image-47159\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/gnome-tweaks-remap-700x538.webp\" alt=\"\" width=\"610\" height=\"469\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/gnome-tweaks-remap-700x538.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/gnome-tweaks-remap-300x231.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/gnome-tweaks-remap-768x590.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/gnome-tweaks-remap.webp 1038w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><p id=\"caption-attachment-47159\" class=\"wp-caption-text\">GNOME Tweaks<\/p><\/div>\n<p><!--more--><\/p>\n<p>Without the tools mentioned above, Linux also provides system config files to manually re-map your keyboard key-codes. And, here I&#8217;m going to show you how to do the trick in Ubuntu (tested in 24.04).<\/p>\n<p><b>NOTE: The method in this tutorial can be only used for simple 1:1 key remaps.<\/b><\/p>\n<h3>Step 1: Find out the scancodes of the keys you want to remap<\/h3>\n<p>When you pressing a physical key on keyboard, it sends a <b>scancode<\/b> to the computer. Then, your system maps the scancode to a virtual <b>keycode<\/b>. By re-mapping the <code>scancode<\/code> to another <code>keycode<\/code> as you want will do the job.<\/p>\n<p><b>1.<\/b> To find out the scancode of the keys, first, press <code>Ctrl+Alt+T<\/code> on keyboard to open up a terminal window. When it opens, run command:<\/p>\n<pre>evtest<\/pre>\n<p>If command not found, run <code>sudo apt install evtest<\/code> to install. And in the terminal output, it will print all the available input devices.<\/p>\n<p>In my case, I have laptop&#8217;s built-in keyboard (AT Translated Set 2 keyboard) and wireless keyboard (YICHIP Wireless Device) using a USB receiver.<\/p>\n<p>As the screenshot below shows, I have to type <b>7<\/b>, the number in <code>\/dev\/input\/event7<\/code>, for the wireless keyboard.<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/evtest.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47160\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/evtest-700x505.webp\" alt=\"\" width=\"610\" height=\"440\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/evtest-700x505.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/evtest-300x216.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/evtest.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>2.<\/b> Once you input the corresponding number and hit enter in last command, you can then <b>hit any key<\/b> on keyboard. The terminal will output the information about what you pressed\/released.<\/p>\n<p>In the screenshot below, the value code (e.g., 70028, 70025, 700e0, 700e2) are the scancodes for the corresponding key press\/release.<\/p>\n<p><b>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.<\/b><\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/getscancode.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47161\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/getscancode-700x505.webp\" alt=\"\" width=\"610\" height=\"440\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/getscancode-700x505.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/getscancode-300x216.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/getscancode.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Step 2: Get the ID of your Keyboard<\/h3>\n<p>Before remapping, you also need to find out the information to identify your keyboard.<\/p>\n<p><b>For USB keyboard<\/b>, simply run the command below in terminal (Ctrl+Alt+T):<\/p>\n<pre>lsusb<\/pre>\n<p>In the output, find out your keyboard according to its name. And write down the ID. It&#8217;s usually xxxx:xxxx, while the first 4 numbers is <strong>vendor ID<\/strong> and later 4 is <strong>product ID<\/strong>.<\/p>\n<p>Also, run the command below to tell the <strong>bus ID<\/strong> (usually 03 in hex):<\/p>\n<pre>cat \/usr\/include\/linux\/input.h |grep BUS_*<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/usb-bus-id.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47162\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/usb-bus-id-700x397.webp\" alt=\"\" width=\"610\" height=\"346\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/usb-bus-id-700x397.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/usb-bus-id-300x170.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/usb-bus-id-768x435.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/usb-bus-id.webp 778w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p><b>If it&#8217;s NOT USB device<\/b>, try running the command below instead to get <strong>the modalias<\/strong>:<\/p>\n<pre>cat \/sys\/class\/input\/event7\/device\/modalias<\/pre>\n<p>In the command, replace <b>event7<\/b> for the correct device according to the <code>evtest<\/code> command in &#8220;Step 1&#8221;. In terminal output, copy the &#8220;input:b****v****p****e****&#8221;<\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/keyboard-modalias.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47163\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/keyboard-modalias-700x278.webp\" alt=\"\" width=\"610\" height=\"242\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/keyboard-modalias-700x278.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/keyboard-modalias-300x119.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/keyboard-modalias.webp 706w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<h3>Step 3: Add udev rule to remap keyboard key<\/h3>\n<p>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.<\/p>\n<p>First, in Ubuntu Linux there&#8217;s built-in <code>\/usr\/lib\/udev\/hwdb.d\/60-keyboard.hwdb<\/code> config file for <code>scancode<\/code> and <code>keycode<\/code> mapping.<\/p>\n<p>However, it&#8217;s recommended to create a rule under <code>\/etc\/<\/code> directory for user custom key mapping.<\/p>\n<p><b>1.<\/b> First, run command in terminal (Ctrl+Alt+T) to create and edit the udev config file:<\/p>\n<pre>sudo gnome-text-editor \/etc\/udev\/hwdb.d\/61-keyboard-local.hwdb<\/pre>\n<p>Here, replace <code>gnome-text-editor<\/code> with <code>gedit<\/code> for 22.04, or <code>mousepad<\/code> for XFCE, <code>pluma<\/code> for MATE, or <code>nano<\/code> command line text editor that works in most desktop environments.<\/p>\n<p><b>2.<\/b> When files opens, add similar lines below:<\/p>\n<pre>evdev:input:b0003v3151p3020e0200*\r\n KEYBOARD_KEY_7001e=reserved\r\n KEYBOARD_KEY_700e2=leftctrl\r\n KEYBOARD_KEY_700e0=leftalt<\/pre>\n<p>The first line <code>evdev:input:b0003v3151p3020e0200*<\/code> is used to identify which keyboard device to remap. While <code>input:b0003v3151p3020e0200<\/code> is the modalias you got in &#8220;Step 2&#8221;.<\/p>\n<p>In my case, it can be replaced with <code>evdev:input:b<b>0003<\/b>v<b>3151<\/b>p<b>3020<\/b>*<\/code>. While, 0003 is the USB bus ID, 3151 is vendor ID, and 3020 is product ID. And, both has * in the end.<\/p>\n<p>And following lines tell to remap the scan-codes to your desired key-codes:<\/p>\n<ul>\n<li><code>KEYBOARD_KEY_7001e=reserved<\/code> &#8211; tells to disable the key whose scancode is &#8220;7001e&#8221;.<\/li>\n<li><code>KEYBOARD_KEY_700e2=leftctrl<\/code> &#8211; tells to remap the key whose scancode is &#8220;700e2&#8221; (Left Alt in my case) to Left Ctrl.<\/li>\n<li><code>KEYBOARD_KEY_700e0=leftalt<\/code> &#8211; remap the key whose scancode is &#8220;700e0&#8221; (Left Ctrl in my case) to Left Alt.<\/li>\n<\/ul>\n<p>The 3rd line tells to remap the key (left alt in my case) whose scancode is &#8220;700e2&#8221; to left ctrl. While the last line remap scancode 700e0 to left alt.<\/p>\n<p><b>For choice, you may replace keycodes (lowercase) by adding &#8216;key_&#8217; at beginning, so it will be &#8216;key_leftctrl&#8217;, &#8216;key_leftalt&#8217;, &#8216;key_1&#8217;, &#8216;key_2&#8217;, &#8216;key_a&#8217;, &#8216;key_b&#8217;, &#8216;key_comma&#8217;, &#8216;key_dot&#8217;, or something similar (evtest command output can tell)&#8230;<\/b><\/p>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/hwdb-keyboard.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-47164\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/hwdb-keyboard-700x289.webp\" alt=\"\" width=\"610\" height=\"252\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/hwdb-keyboard-700x289.webp 700w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/hwdb-keyboard-300x124.webp 300w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/hwdb-keyboard-768x317.webp 768w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/hwdb-keyboard.webp 1024w\" sizes=\"auto, (max-width: 610px) 100vw, 610px\" \/><\/a><\/p>\n<p>You can add as many lines as you want with <code>KEYBOARD_KEY_scancode=keycode<\/code> layout. While, each line has a blank space at beginning.<\/p>\n<p>After editing, save file. For nano text editor, press Ctrl+S to save, then Ctrl+X to exit.<\/p>\n<h3>Step 4: Apply changes<\/h3>\n<p>After editing udev rule to remap keyboard keys, you can run the commands below one by one to apply:<\/p>\n<ul>\n<li>First, update the hardware database index:\n<pre>sudo systemd-hwdb update<\/pre>\n<\/li>\n<li>Next, reload the new key mappings:\n<pre>sudo udevadm trigger<\/pre>\n<\/li>\n<\/ul>\n<p><b>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.<\/b><\/p>\n<p>Finally, try to either hit the keys on keyboard to see the changes, or use the command below<\/p>\n<pre>udevadm info \/dev\/input\/by-id\/* | grep KEYBOARD_KEY<\/pre>\n<p><a href=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apply-keycode-changes.webp\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-47165\" src=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apply-keycode-changes.webp\" alt=\"\" width=\"666\" height=\"354\" srcset=\"https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apply-keycode-changes.webp 666w, https:\/\/ubuntuhandbook.org\/wp-content\/uploads\/2024\/09\/apply-keycode-changes-300x159.webp 300w\" sizes=\"auto, (max-width: 666px) 100vw, 666px\" \/><\/a><\/p>\n<p><b>The remapped keys will persistent after reboot<\/b>, because the <code>systemd-hwdb-update.service<\/code> includes a trigger <code>ConditionNeedsUpdate=\/etc<\/code> that automatically update hardware database index, and kernel auto-reload it on system boot.<\/p>","protected":false},"excerpt":{"rendered":"<p>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, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":40367,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[2208],"class_list":["post-47158","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-howtos","tag-keyboard"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/comments?post=47158"}],"version-history":[{"count":0,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/posts\/47158\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media\/40367"}],"wp:attachment":[{"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/media?parent=47158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/categories?post=47158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ubuntuhandbook.org\/index.php\/wp-json\/wp\/v2\/tags?post=47158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}