General keyboard layout solution

Hello,

I would like here to share a trick I would love to have found in the documentation.
And maybe the OnlyKey team will add to the documentation.

  1. Who can benefit from it ?
  • UNIX users with keyboard layout not available in the OnlyKey layout list.
  • UNIX users using there OnlyKey on different computer UNIX or not with different keyboard layout.

Then you can configure your OnlyKey to the most common keyboard layout where you live to be able to use it on most computer around you even Windows and Mac.
And have your computer with a different keyboard layout handle transparently the situation.

For non UNIX user may be the same principle can be applied for M$ Window$ or the fruit company or not. But some one else need to adapt it.

  1. The principle behind the trick

At least with Xorg, all connected keyboard does not need to share the same layout.
Then I searched for a solution to keep my specially tailored to my needs keyboard layout totally incompatible with all layout available in the OnlyKey.
And simultaneously configure X to set only the OnlyKey keyboard layout to the type the OnlyKey need. And the solution exist with the command line:

setxkbmap -device $ONLYKEY_ID -layout $ONLYKEY_KEYBOARD_LAYOUT

ONLYKEY_KEYBOARD_LAYOUT must be the layout you want to assign to the OnlyKey:

  • “us” for standard English US QWERTY keyboard layout
  • “fr” for standard French AZERTY keyboard layout
  • and so on …

ONLYKEY_ID must be set to the “xinput” id of the OnlyKey that you can find with the eponymous command:

$ xinput
⎡ Virtual core pointer                          id=2    [master pointer  (3)]
⎜   ↳ Virtual core XTEST pointer                id=4    [slave  pointer  (2)]
⎜   ↳ TPPS/2 IBM TrackPoint                     id=12   [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)]
    ↳ Video Bus                                 id=7    [slave  keyboard (3)]
    ↳ Sleep Button                              id=8    [slave  keyboard (3)]
    ↳ Integrated Camera: Integrated C           id=9    [slave  keyboard (3)]
    ↳ AT Translated Set 2 keyboard              id=10   [slave  keyboard (3)]
    ↳ ThinkPad Extra Buttons                    id=13   [slave  keyboard (3)]
    ↳ CRYPTOTRUST ONLYKEY                       id=14   [slave  keyboard (3)]
∼ SynPS/2 Synaptics TouchPad                    id=11   [floating slave]

And all the work can be automated in a script trigger on OnlyKey plug event with a “udev” rule

  1. The automatic solution.

First create a script somewhere in your $HOME directory and call it something like ‘set_onlykey_layout.sh’
The content of the script should be:

#!/bin/sh                                                                       
                                                                                
# This script assign all your connected OnlyKeys the keyboard layout of your    
# choice. Without changing the layout of your others keyboards.                 
#  
# Just set the ONLYKEY_KEYBOARD_LAYOUT just below to the same layout that is
# configured in your keys.
# And the ONLYKEY_USER which will use it.
ONLYKEY_KEYBOARD_LAYOUT='us'  # or 'fr' or whatever is configured in your OnlyKey
ONLYKEY_USER='Your UNIX login name'

# This look for the first local X display available on the host.
# Work in most usual case but can be improved for rare tricky situations.      
DISPLAY=$(ls /tmp/.X11-unix | grep '^X[0-9][0-9]*$' | tr 'X' ':' | head -n 1)  

# As this script may be trigger by the USB connection event,
# wait a little bit for initialisation to be done.
sleep 1

# Needed for those vars to be available in the 'heredoc' below.
export DISPLAY ONLYKEY_KEYBOARD_LAYOUT

# Needed to get access rights to the DISPLAY
/bin/su $ONLYKEY_USER << 'EOF'
   EXTRACT_XINPUT_DEVICE_ID='s/.*\sid=\([1-9][0-9]*\)\s.*/\1/1'

   ONLYKEY_ID_LIST=$(xinput | grep "ONLYKEY" | sed $EXTRACT_XINPUT_DEVICE_ID)  

   for ONLYKEY_ID in $ONLYKEY_ID_LIST
   do
      setxkbmap -device $ONLYKEY_ID -layout $ONLYKEY_KEYBOARD_LAYOUT
   done
EOF

Just change the ONLYKEY_KEYBOARD_LAYOUT and ONLYKEY_USER to the value matching your case.
Then make the script executable with the command:

chmod u+x set_onlykey_layout.sh

Finally add the udev rule:

ACTION=="add", SUBSYSTEM=="input", ATTRS{idVendor}=="1d50", ATTRS{idProduct}=="60fc", RUN+="/usr/bin/at -M -f '/home/Your_unix_login_name/set_onlykey_layout.sh' now"

At the end of your /etc/udev/rules.d/49-onlykey.rules file.
and replace ‘/home/Your_unix_login_name/set_onlykey_layout.sh’ by the real path of the script you have just created.

NOTE: Ensure that the package ‘at’ containing the command ‘/usr/bin/at’ is installed on your system. If not install it with your preferred package manager.

And “Voila !!!” you have a plug and play OnlyKey with its own keyboard layout independent of your main keyboard layout.
If you have more than one OnlyKey user on this computer you can place a script in the $HOME directory of each user and add an udev rule for each script.

That’s all folks !!!

2 Likes