Saturday, January 11, 2020

How to remap keys under Linux for a specific keyboard only


I've recently bought a Unicomp keyboard that comes with swapped right-alt and Windows keys. The keyboard identifies like this on lsusb:


Bus 003 Device 002: ID 17f6:0822 Unicomp, Inc

Is there a way to have the kernel (i.e. not xmodmap-based) swap the right-alt and windows keys so every application sees them in the swapped places even if they get raw keyboard input (swapping stuff with xmodmap won't do that)? Is there a way to have that only for this one keyboard?


Answer



Yes, it's possible using XKB. Unlike xmodmap, XKB can remap your keys for individual devices.


Note: Make sure you have xkbcomp > 1.2.0


First list your devices with:


xinput list


You'll get something like this:


⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜ ↳ Virtual core XTEST pointer id=4 [slave pointer (2)]
⎜ ↳ Wacom Bamboo Pen Pen stylus id=11 [slave pointer (2)]
⎜ ↳ Wacom Bamboo Pen Finger touch id=12 [slave pointer (2)]
⎜ ↳ Logitech USB-PS/2 Optical Mouse id=13 [slave pointer (2)]
⎜ ↳ Wacom Bamboo Pen Pen eraser id=14 [slave pointer (2)]
⎜ ↳ Wacom Bamboo Pen Finger pad id=15 [slave pointer (2)]
⎜ ↳ GASIA USB KB V11 id=17 [slave pointer (2)]
⎣ Virtual core keyboard id=3 [master keyboard (2)]
↳ Virtual core XTEST keyboard id=5 [slave keyboard (3)]
↳ Power Button id=6 [slave keyboard (3)]
↳ Power Button id=7 [slave keyboard (3)]
↳ G19 Gaming Keyboard id=8 [slave keyboard (3)]
↳ G19 Gaming Keyboard id=9 [slave keyboard (3)]
↳ Logitech G19 Gaming Keyboard id=10 [slave keyboard (3)]
↳ GASIA USB KB V11 id=16 [slave keyboard (3)]

Identify the string of your device and edit the following shell script, changing the sed line with one that fits your device's name. Then change the keys you need remapped.


Example: Load xev and press a key you want to remap. Suppose you find out it's keycode 84. Lookup 84 in https://gist.github.com/zoqaeski/3880640. The key name there is . Then lookup the key you want it replaced by (in the same link, farther below) and copy what's inside the brackets. Repeat the process for all the keys you want.


remote_id=$(
xinput list |
sed -n 's/.*GASIA.*id=\([0-9]*\).*keyboard.*/\1/p'
)
[ "$remote_id" ] || exit
# remap the following keys, only for my custom vintage atari joystick connected
# through an old USB keyboard:
#
# keypad 5 -> keypad 6
# . -> keypad 2
# [ -> keypad 8
# left shift -> left control
mkdir -p /tmp/xkb/symbols
# This is a name for the file, it could be anything you
# want. For us, we'll name it "custom". This is important
# later.
#
# The KP_* come from /usr/include/X11/keysymdef.h
# Also note the name, "remote" is there in the stanza
# definition.
cat >/tmp/xkb/symbols/custom <<\EOF
xkb_symbols "remote" {
key { [ KP_Right, KP_6, U2192, U21D2 ] };
key { [ KP_Down, KP_2, U2193, U21D3 ] };
key { [ KP_Up, KP_8, U2191, U21D1 ] };
key { [ Control_L ] };
};
EOF
# (1) We list our current definition
# (2) Modify it to have a keyboard mapping using the name
# we used above, in this case it's the "remote" definition
# described in the file named "custom" which we specify in
# this world as "custom(remote)".
# (3) Now we take that as input back into our definition of the
# keyboard. This includes the file we just made, read in last,
# so as to override any prior definitions. Importantly we
# need to include the directory of the place we placed the file
# to be considered when reading things in.
#
# Also notice that we aren't including exactly the
# directory we specified above. In this case, it will be looking
# for a directory structure similar to /usr/share/X11/xkb
#
# What we provided was a "symbols" file. That's why above we put
# the file into a "symbols" directory, which is not being included
# below.
setxkbmap -device $remote_id -print \
| sed 's/\(xkb_symbols.*\)"/\1+custom(remote)"/' \
| xkbcomp -I/tmp/xkb -i $remote_id -synch - $DISPLAY 2>/dev/null

Then source it (you can add it to your .xinitrc). All done! Now pressing the keys should generate the desired output, only for the device you specified.


Edit: Recently, I've noticed that, for some reason, the new configuration isn't applied immediately. You must first press a key on your other keyboard, then test the configured keys on your modified keyboard. I don't know why this happens, maybe some sort of cache.


No comments:

Post a Comment

hard drive - Leaving bad sectors in unformatted partition?

Laptop was acting really weird, and copy and seek times were really slow, so I decided to scan the hard drive surface. I have a couple hundr...