After a little research, I've found a solution, although I'm still open to other (probably better) answers.
Here's a startup script (which can be added to Startup Applications) which will set the maually entered usbkbd_layout variable to the usbkbd device ids found in the xinput -list:
#!/bin/bash
usbkbd=`xinput -list | grep -c "USB Keyboard"`
if [[ "$usbkbd" -gt 0 ]]
then
usbkbd_ids=`xinput -list | grep "USB Keyboard" | awk -F'=' '{print $2}' | cut -c 1-2`
usbkbd_layout="tr(f)"
for ID in $usbkbd_ids
do
setxkbmap -device "${ID}" -layout "${usbkbd_layout}"
done
fi
exit 0
This script is quite useful (and more stable) for scenarios where user starts using the laptop on a desktop setup (with external keyboard, mouse and monitor, etc.), and it can also be run manually whenever the external USB keyboard is plugged in...
==========================================================================
THE BETTER (almost perfect) SOLUTION - found thanks to MinimusHeximus and the respective contributors to the thread he mentioned in his comment below:
I can now just plugin my USB keyboard and automatically have its different (TR-F) keyboard layout applied while still keeping the default keyboard layout (TR-Q) on my laptop!
Here are the files and their contents that make this possible:
/etc/udev/rules.d/00-usb-keyboard.rules
ATTRS{idVendor}=="09da", ATTRS{idProduct}=="0260", OWNER="sadi"
ACTION=="add", RUN+="/home/sadi/.bin/usb-keyboard-in_udev"
ACTION=="remove", RUN+="/home/sadi/.bin/usb-keyboard-out_udev"
/home/sadi/.bin/usb-keyboard-in_udev
#!/bin/bash
/home/sadi/.bin/usb-keyboard-in &
/home/sadi/.bin/usb-keyboard-in
#!/bin/bash
sleep 1
DISPLAY=":0.0"
HOME=/home/sadi/
XAUTHORITY=$HOME/.Xauthority
export DISPLAY XAUTHORITY HOME
usbkbd_id=`xinput -list | grep "USB Keyboard" | awk -F'=' '{print $2}' | cut -c 1-2 | head -1`
usbkbd_layout="tr(f)"
if [ "${usbkbd_id}" ]; then
gsettings set org.gnome.settings-daemon.plugins.keyboard active false
sleep 2
setxkbmap -device "${usbkbd_id}" -layout "${usbkbd_layout}"
fi
/home/sadi/.bin/usb-keyboard-out_udev
#!/bin/bash
/home/sadi/.bin/usb-keyboard-out &
/home/sadi/.bin/usb-keyboard-out
#!/bin/bash
sleep 1
DISPLAY=":0.0"
HOME=/home/sadi/
XAUTHORITY=$HOME/.Xauthority
export DISPLAY XAUTHORITY HOME
gsettings set org.gnome.settings-daemon.plugins.keyboard active true
Notes:
- Of course all of the four files in your ."bin" folder should have necessary permissions (readable and executable) which maybe
implemented for example with a Terminal command like
chmod - 755
/home/sadi/.bin/usb-keyboard-*
- Sometimes after the USB keyboard is plugged in it still uses the same (default) keyboard layout, and switches to the specified layout upon the second try (perhaps requiring a little more sleep time somewhere?)
- The USB keyboard specific layout is not effective in the login screen (when you Log Out).
- If you use a separate partition for /home, then it might be a better idea to put the four scripts somewhere in the root partition, e.g. /usr/local/bin and modify the contents of all respective files accordingly as sometimes udev may look for those files before your /home partition is mounted and cause problems.
IN ORDER TO ADAPT THIS SETUP TO DIFFERENT REQUIREMENTS:
- USB keyboard vendor and product ids should be changed as per the output of the command
lsusb
(For example, my lsusb
output have this for my USB Keyboard: Bus 001 Device 006: ID 09da:0260 A4 Tech Co., Ltd
)
- OWNER and all user directory names should be changed from "sadi" to another name
- The usbkbd_id may require a little adjustment to grab the correct device id (For example, output of the commands
xinput -list | grep "USB Keyboard"
gives me two lines; ↳ USB Keyboard id=14 [slave keyboard (3)]
and ↳ USB Keyboard id=16 [slave keyboard (3)]
; which are then filtered by awk
using "=" as field delimiter and capturing the second part; then cutting only the first two digits, and then using only the value in the first line)
- The value for usbkbd_layout may be any other valid choice
Best Answer
At log in the
.Xmodmap
(set up as user or globally) would read properly. However, when the monitor goes to sleep, and I log back in, it would be reset and keys would work as before.Xmodmap
was loaded. No setting worked around until at some point I realized that my keyboard is plugged in to the monitor and thus all sittings are lost when the monitor goes to sleep or is turned off. What helped was to plug in the keyboard directly to the computer tower. Now the initial reading of.Xmodmap
right after logging in is maintained independently of the monitor being on or off.