Last active
February 8, 2024 09:33
-
-
Save sapristi/3e2abfa57a0b2e07ba8c20e678add241 to your computer and use it in GitHub Desktop.
Udev rule and script for automatic keyboard layout settings
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# /etc/udev/rules.d/00-keyboard.rules | |
ACTION=="add", ATTR{name}=="Bastard Keyboards Charybdis Nano", ATTR{phys}=="*/input0", OWNER="sapristi", RUN+="/usr/local/bin/usb-keyboard-udev Charybdis" | |
NOTE: | |
on ubunut, use ACTION=="bind", otherwise device may not be present !! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# /usr/local/bin/usb-keyboard | |
SETXKBMAP=/usr/bin/setxkbmap | |
XINPUT=/usr/bin/xinput | |
KBNAME=$1 | |
XKBLAYOUT="us" | |
XKBVARIANT="altgr-intl" | |
echo Setting layout $XKBLAYOUT $XKBVARIANT for $KBNAME | |
USERLIST=$(w -h | cut -d' ' -f1 | sort | uniq) | |
for CUR_USER in ${USERLIST}; do | |
CUR_USER_XAUTH="$(sudo -Hiu ${CUR_USER} env | grep -e "^HOME=" | cut -d'=' -f2)/.Xauthority" | |
# CUR_USER_XAUTH="/home/${CUR_USER}/.Xauthority" | |
CUR_USER_DISPL=":0" | |
export XAUTHORITY="${CUR_USER_XAUTH}" | |
export DISPLAY="${CUR_USER_DISPL}" | |
KEYBOARDS=$(/usr/bin/xinput list --short | grep $KBNAME | grep keyboard | grep -v Control | sed -e 's/^.*id=\([0-9]\+\).*/\1/g') | |
if [ -n "$KEYBOARDS" ]; then | |
for ID in $KEYBOARDS; do | |
CMD="/usr/bin/setxkbmap -device $ID -layout $XKBLAYOUT -variant $XKBVARIANT" | |
echo Running \"${CMD}\" | |
$($CMD) | |
done | |
else | |
echo "No keyboard found" | |
fi | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/sh | |
# /usr/local/bin/usb-keyboard-udev | |
LOG=/var/log/usb-keyboard.log | |
command=$(dirname $0)/usb-keyboard | |
echo $command $@ >> $LOG | |
[ -x "$command" ] && $command "$@" >>$LOG 2>&1 & |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment