#!/usr/bin/env bash
# Reset our keyboards back to GB to start with ...
setxkbmap -layout us && setxkbmap -layout gb
# The remote control device (USB keyboard)
# has keys that overlap with standard keyboards.
# Therefore we map the offending keys here.
kbd_id() {
keyboard="$1"
xinput list |
sed -n "s/.*$keyboard.*id=\([0-9]*\).*keyboard.*/\1/p" |
head -n1
}
# In our case the USB:ID was shown in the `xinput list` output,
# but this is unusual and you may have to match
# on names or even correlate with /dev/input/by-id/*
remote_id=$(kbd_id 'HID 04d9:1203')
[ "$remote_id" ] || exit
# re-execute this script as "$xuser" if that's not root, in case the root user is not permitted to access the X session.
xuser=$(ps -C Xorg -C X -ouser=)
[ $(id -un) = root ] && [ "$xuser" != root ] && exec su -c $0 "$xuser"
export DISPLAY=:0
# Write out the XKB config to remap just
# the keys we're interested in
mkdir -p /tmp/xkb/symbols
cat >/tmp/xkb/symbols/custom <<\EOF
xkb_keycodes {
<NMLK> = 230;
<KPDV> = 231;
<KPMU> = 232;
};
EOF
# Amend the current keyboard map with
# the above key mappings, and apply to the particular device.
# Note xkbcomp >= 1.2.1 is needed to support this
setxkbmap -device $remote_id -print |
sed 's/\(xkb_keycodes.*\)"/\1+custom"/' |
xkbcomp -I/tmp/xkb -i $remote_id -synch - $DISPLAY 2>/dev/null