How to remap a regular key into a modifier key (Shift…) in Linux

On my new laptop keyboard, there’s a strange key next to my shift key. Frankly it’s useless to me and having a shorter shift key doesn’t let me reach some of the combinations that I was used to. I’d like to turn it that key into a left shift as well so that I have one long left shift again.

weird layout

First I’ve tried xmodmap according to this answer, and same as him, I’ve had no success.

Then I tried to create a new xmodmap config by copying the entry of the left shift key into the strange key’s row. Applying the settings however took very long (>10s) so having that in my bashrc was not an option, since it made opening new terminal tabs take way too long. It also didn’t work in X either, only terminals. I also read that xmodmap is mostly considered outdated.

Lastly I tried to use xkb but frankly couldn’t really figure out how according to entries on the Arch wiki and here.

Asked By: henry_the_brave

||

You can remap scancodes to keycodes with udev, this works whether you are using xorg, wayland or the virtual console.

The scan code can be obtained with the evtest command. Select your keyboard when evtest ask for device to scan, and press the key you want to remap. Here is a sample output when alt key is pressed

Event: time 1688218985.587133, type 4 (EV_MSC), code 4 (MSC_SCAN), value 700e2
Event: time 1688218985.587133, type 1 (EV_KEY), code 29 (KEY_LEFTALT), value 1

Then create an hwdb file that maps scancodes to keycodes in /etc/udev/hwdb.d.

This file remaps CapsLock to Shift, for example.

# generic keyword
evdev:input:b0003v*p*
  KEYBOARD_KEY_70039=shift

Finally, run the following command to apply the change.

systemd-hwdb update
udevadm trigger

See Map scancodes to keycodes for detailed instructions and more examples.

Note:

Answered By: memchr

@memchr’s answer was a good start but it wasn’t clear enough to me

I don’t know all intricacies of the configuration. In the end, that’s just what worked for me.

  • sudo touch /etc/udev/hwdb.d/mx-keys.hwdb – create a config file (you can name your file anything you like as long as it has .hwdb extension)
  • sudo evtest – select your keyboard (install evtest if needed).
    Press the key you want to remap (in my case, > key next to left Shift) and you will receive the similar output
Event: time 1692951945.797570, type 4 (EV_MSC), code 4 (MSC_SCAN), value 70064
Event: time 1692951945.797570, type 1 (EV_KEY), code 86 (KEY_102ND), value 1
  • sudo nano /etc/udev/hwdb.d/mx-keys.hwdb – open config file in the editor

This is the content of my file

evdev:name:Logitech MX Keys:*
  KEYBOARD_KEY_70064=leftshift

Let’s break it down

  • Ligitech MX Keys – name of my keyboard, you can copy it from the list of devices in evtest

For the next line we will use the output on key press from evtest

  • 70064 – value of the key you want to remap. Take it from the end of the first line
  • leftshift – value of the key you want to remap to. Take it from the second line, for me it was KEY_LEFTSHIFT

After that save the file and run these commands to apply the changes

systemd-hwdb update
udevadm trigger

In case you did everything right, the key will work right away, no need to restart the computer

Additional source: reddit post

Answered By: iamkirill
Categories: Answers Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.