How to remap key in Ubuntu 17.10 Wayland? (Up key to Shift)

7,549

Solution 1

After around 2 days of googling an answer to the question "how to remap keys on wayland" I have managed to find a very, very simple way. I dont know if its the right way but it actually works.

The only thing that I had to do is open the file

/usr/share/X11/xkb/keycodes/evdev

That file lists all the keycodes for all the keys in the form < code > = number

You can simply swap the keycodes around. What I actually swapped around is the numbers NOT the code. I changed backslash which on dvorak is next to Enter with Ctrl for Emacs.

edit: to find out which "code" represents each key as in < code > = number you can run the xev command from the terminal to get the keycode printed in the terminal after pressing the button (search for that keycode in the evdev file to get the evdev scancode for the key), or you can visit

/usr/share/X11/xkb/symbols/

This is a directory which has one file for each language installed on the computer and modifier keys. Select the language your pc runs on.

In that file there will be all variants supported listed, such as dvorak, programmers dvorak, colemans etc etc.

There you can see the < code > names

Solution 2

Right now customizing keys is a bit tricky when using Wayland: https://unix.stackexchange.com/questions/292868/how-to-customise-keyboard-mappings-with-wayland

But if you just want to remap a key so that it should function as another key, the command setkeycodes seems to work.

Run sleep 1; showkey -s in the terminal as root, and then press the key Up once, and then wait 10 seconds

root@laptop:~# sleep 1; showkey -s
kb mode was ?UNKNOWN?
[ if you are trying this under X, it might not work
since the X server is also reading /dev/console ]

press any key (program terminates 10s after last keypress)...
^[[A0xe0 0x48 
0xe0 0xc8 
root@laptop:~# 

From the output we see that 0xe0 0x48 is the scan code for the key Up.

Run sleep 1; showkey -k in the terminal as root, and then press the key Right shift once, and then wait 10 seconds

root@laptop:~# sleep 1; showkey -k
kb mode was ?UNKNOWN?
[ if you are trying this under X, it might not work
since the X server is also reading /dev/console ]

press any key (program terminates 10s after last keypress)...
keycode  54 press
keycode  54 release
root@laptop:~# 

From the output we see that 54 is the key code for the key Right shift.

To remap the Up key to the Right shift key, run setkeycodes 0xe048 54 in the terminal as root.

root@laptop:~# setkeycodes 0xe048 54

The scan code was previously printed as 0xe0 0x48, but now when it was given as an input argument to setkeycodes, it was written 0xe048.

Share:
7,549

Related videos on Youtube

Aaron Skomra
Author by

Aaron Skomra

Updated on September 18, 2022

Comments

  • Aaron Skomra
    Aaron Skomra over 1 year

    I have a Lenovo Yoga 710 where the 'Up' key is in the place where the shift key should be. Unfortunately this laptop won't run earlier kernels because it has "new" hardware. I see the mechanism to remap keys in Wayland (GNOME) here: https://superuser.com/questions/1196241/how-to-remap-caps-lock-on-wayland

    But, I can't find the xkb-options code for "Up" anywhere. "Right shift" might be "rshift" according to /usr/share/X11/xkb/rules/base. Where is the list of xkb-options key names and/or what are the names for the 'right shift key' and the 'Up' key?

  • Ace
    Ace about 6 years
    is this persistent through reboots? is there a process for resetting things back to normal?
  • Erik Sjölund
    Erik Sjölund about 6 years
    A reboot will give you back the original key mapping. I usually run the setkeycodes command after each reboot.
  • jeremyh
    jeremyh almost 4 years
    Thank you. Worked perfectly on Fedora too :)
  • Flimm
    Flimm almost 3 years
    Some lines look like <I171> = 171; // #define KEY_NEXTSONG 163. How should lines like these be edited, specically the define section?
  • user598527
    user598527 over 2 years
    Appears to require logging out (or restarting the system). I was able to remove the entire line for the corresponding key. Fedora 34 on Wayland.