42

This is working for the current session:

costales@dev:~/Desktop$ xmodmap -e "keycode 166 = less"
costales@dev:~/Desktop$ xmodmap -e "keycode 167 = greater"

Then I created this config file:

costales@dev:~/Desktop$ cat ~/.Xmodmap 
keycode 166 = less
keycode 167 = greater
costales@dev:~/Desktop$ 

But it's not working after a reboot. How could I force the remap to survive reboot?

Costales
  • 2,318

9 Answers9

30

In my long experience with remapping keys in Ubuntu, the permanent solution is to modify a file called evdev in /usr/share/X11/xkb/keycodes.

Run this command in the command prompt.

sudo gedit /usr/share/X11/xkb/keycodes/evdev

Remember gedit is a Ubuntu text editor so you can use nano or vim instead of gedit.

The file is in the format: ALIAS: CODE. You can swap buttons by interchanging codes of different keys. For example, to swap RCONTROL with RETURN, edit two lines in evdev into this:

<RTRN> = 105;

<RCTL> = 36;

To know the number code for a button run xev on the terminal.

Log out and back in to apply changes.

jtpereyda
  • 2,115
  • 3
  • 20
  • 21
GilbertS
  • 401
19

I've been using 16.04 for a little bit and it seems to use sddm as its desktop manager. It used to be LightDM and GDM before that. Both its predacessors are documented to load ~/.Xmodmap automatically but I can't find anything explicitly saying sddm does.

Therefore it may be advisable to just force it to load with a new script. You just need to run xmodmap ~/.Xmodmap and you can do that a number of ways:

There are probably a few dozen other ways to manage this, essentially doing the same thing.

Oli
  • 299,380
9

I added a file /etc/X11/Xsession.d/80_xmodmap with these contents:

#!/bin/sh
# Set custom keycodes
#
# This file is sourced by Xsession(5), not executed.
# The "|| true" is to ensure that the Xsession script does not terminate on error

USRMODMAP="$HOME/.Xmodmap"

if [ -x /usr/bin/xmodmap ]; then
        if [ -f "$USRMODMAP" ]; then
                /usr/bin/xmodmap "$USRMODMAP" || true
        fi
fi

This works every time.

David Foerster
  • 36,890
  • 56
  • 97
  • 151
5

Go to Startup Applications and make a new entry with the content shown in the following screenshot:

adding new startup application(command)

It works every time.

4

After searching on Google, I was able to resolve my laptop faulty keys remapping. All faulty keys on my Thinkpad T400 are working fine after remapping. Also, it's permanent without an extra load on the CPU core.

  • First find your keyboard layout or keycode using below command.

    xev -event keyboard 
    
  • For example, on my Thinkpad T400 keycode found as follows:

    Keycode of "z" key is 52
    Keycode of "x" key is 53
    Keycode of "c" key is 54
    Keycode of "v" key is 55
    Keycode of "F6" key is 72
    Keycode of "F10" key is 76
    Keycode of "F11" key is 95
    Keycode of "F12" key is 96 
    
  • Remapping character keys "z" "x" "c" "v" by assigning function keys "F6" "F10" "F11" "F12".

  • Modify lines as below (Backup file before making any changes to rollback easily if things go wrong).

    sudo vi /usr/share/X11/xkb/keycodes/evdev
    
  • Find the lines and change value as below:

    <AB01> = 72;
    <AB02> = 76;
    <AB03> = 95;
    <AB04> = 96;
    
  • Comment these lines or remove the lines:

    //  <FK06> = 72;
    //  <FK10> = 76;
    //  <FK11> = 95;
    //  <FK12> = 96;
    
  • Save and exit.

To remap special keys:

  • Backup file before making changes to rollback if things go wrong.

    cd /usr/share/X11/xkb/symbols/
    
    sudo vi pc
    
  • Add or modify below lines in function "xkb_symbols "editing" {"

    key [SCLK] {    [  Prior        ]   };
    key [PAUS] {    [  Next         ]   };
    
  • Save and exit.

  • Clear cache.

    sudo rm -rf /var/lib/xkb/*
    
  • Reboot.

4

open gnome-session-properties. add command and name. desktop file will be created in ~/.config/autostart.

remenber: use absolute path, eg /home/xxx/.Xmodmap. never use '~' as your home dir.

eexpress
  • 269
2

Navigate to

/usr/share/X11/xkb/symbols

With sudo privileges, in an editor, open the layout you desire to edit. Replace the values.

After reboot, the changes will be applied.


Here is an example I use to remap my keypad because of keyboard hardware issues.

keypad remap example

quaeched
  • 143
1

Graphical Editor

gnome-tweaks > Keyboard & Mouse > Additional Layout Options

Key Maps

Jonathan
  • 3,984
0

Because .Xmodmap files are much easier to edit than xkb, I use .Xmodmap as a 'source' file, then convert it to xkb (the current standard) and auto-load at boot using the .bashrc method.

It's a few simple steps posted here.

Works perfectly, and using xkb future-proofs against when support for .Xmodmap is dropped.

u2n
  • 923
  • 2
  • 10
  • 22