Ubuntu – Make a wireless keyboard always use specified layout

keyboardkeyboard-layout

This really should be a comment on a reply to the question How to permanently assign a different keyboard layout to a USB keyboard? Since commenting isn't possible, here is the new question, I hope @Sadi is reading it:

My wireless keyboard and mouse stopped working after this. It might be related to the fact that the string "USB Keyboard" isn't contained in the name of my device.

lsusb
Bus 003 Device 088: ID 046a:010b Cherry GmbH

xinput -list
⎡ Virtual core pointer                      id=2    [master pointer  (3)]
⎜   ↳ MLK Wireless Desktop                  id=15   [slave  pointer  (2)]
⎣ Virtual core keyboard                     id=3    [master keyboard (2)]
 ↳ MLK Wireless Desktop                     id=14   [slave  keyboard (3)]

thus my GREP line looks like this:

xinput -list | grep 'Wireless Desktop.*keyboard' "$@"

To change the layout simply note the ID and type

setxkbmap -device 14 {language code}

Any Ideas how to fix it permanently so the computer recognizes this device and has it with the correct language ready once plugged in??


Possible clues here:

Best Answer

As far as I know (which is not much) all solutions involving udev.rules stopped working recently, and at the moment the only solution that seems to work is a simple startup script that assumes the second keyboard is connected before the user logs in or else the user should manually run that script later.

The script tested to work for USB keyboards (here: https://askubuntu.com/a/337431/47343) should of course be modified for wireless keyboards, which might be something like the one below.

Note 1: In addition to entering the keyboard layout code desired, it might also require some trial-and-error (e.g. first entering each line in a terminal window) as the author is an "amateur" (and almost beginner-level) "script writer" ;-)

#!/bin/bash
extkbd=`xinput -list | grep -c "Wireless.*keyboard"`
if [[ "$extkbd" -gt 0 ]]
then
    extkbd_id1=`xinput -list | grep "Virtual core keyboard" | awk -F'=' '{print $2}' | cut -c 1-2`
    extkbd_id2=`xinput -list | grep "Wireless.*keyboard" | awk -F'=' '{print $2}' | cut -c 1-2`
    extkbd_layout="<kbdlayoutcode>"
    setxkbmap -device "${extkbd_id1}" -layout "${extkbd_layout}"
    setxkbmap -device "${extkbd_id2}" -layout "${extkbd_layout}"
    notify-send -i input-keyboard "Wireless Keyboard" "is ready and set for use..."
fi
exit 0

Note 2: As the system seem to assign 2 device IDs to one hardware, I've found it is safer to assign the same keyboard layout code to both of those device IDs.

Related Question