How to Remap Keyboard Keys on Lubuntu

I've installed Lubuntu on an old Mac and found out that by default the Command key is mapped as Super and Control and Control. My muscle memory on this device is to use Command key as a primary control key, so I wanted to remap it for ease of use with Lubuntu. Lubuntu is a lightweight distribution of Linux, designed for older devices. It comes with the LXQT desktop management. This is the part that makes the key remapping a bit trickier. Through some trial and error I have found a way to remap the keys so sharing the know how in case it helps others.

Update LXQt Shortcut

The first tricky part of the set up is the preset shortcuts that Lubuntu installs, namely the Super key is configured to show the main menu. The shortcut need to be updated first, so it doesn't interfere with the remapping. To do that:

  1. Click on the Main Menu
  2. Navigate to Preferences > LXQt Settings > Shortcut Keys
  3. Locate a shortcut for Show/hide main menu
  4. Modify the shortcut, updating it to a key combination, for example Control + M
  5. Note do not update it to either Super or Command one key shortcut, as that will make the remapping hard to work with

Update XModMap

The core of Lubuntu is Ubuntu/Debian and uses XmodMap to map the keys, so we can use it to remap the keys.

  1. Create a file .xmodmap in the home directory
  2. Add remapping instructions into the file and save
  3. Run xmodmap ~/.xmodmap or /usr/bin/xmodmap ~/.xmodmap in the terminal to activate your remapping
  4. Test your Control and Command keys and the combination shortcuts

This is the code that worked for me in the end. I have a keyboard with one Control and two Command/Super keys, which are remapped to two Control and one Super key. So running this remapping twice, will throw an expected error in the terminal, as Super_R cannot be cleared a second time after it has been cleared once.

clear control
clear mod4

remove mod4 = Super_L NoSymbol Super_L Hyper_L Super_R NoSymbol Super_R
remove control = Control_L Control_R Control_L NoSymbol Control_L

keycode 37 = Super_L
keycode 206 = Control_L
keycode 133 = Control_R
keycode 134 = Control_L

add control = Control_L
add control = Control_R
add mod4 = Super_L

Helpful Resources

Helpful Utilities

  • xev command shows the box that will detect key codes; renders a lot of information about each keystroke
  • xinput list command shows the list of devices, get the KEYBOARD_ID from the list, then run xinput test $KEYBOARD_ID this will render the key code of the hit key
  • setxkbmap -option command will reset the mapping back to default; helpful to do a quick reset without a reboot
  • xmodmap -pke command will show all current mappings for the keyboard
  • xmodmap -pke | grep -P '(Control|Super)' command will show specific mappings for the Control and Super keys
  • xmodmap -pm command will show modifier keys

Load Custom Map on Reboot

Once the XModMap is configured and working, but it's not quite the end because the settings will not stick on the reboot. Adding the command to .profile or .bashrc may not work until you launch a terminal. To solve this problem, we can utilize the Lubuntu's LXQt autostart configuration to execute our command.

  1. Click on the Main Menu
  2. Navigate to Preferences > LXQt Settings > Session Settings
  3. Inside the dialog box, click on Autostart
  4. Click Add to add a new command
  5. Give command the name xmodmap and add /usr/bin/xmodmap ~/.xmodmap as a command
  6. Save and close

Now the code should load on boot time and your keyboard keys should be remapped.

Happy coding.