I wrote this script to change the usb keyboard layout automatically on plug-in
import pyudev
from subprocess import call
monitor = pyudev.Monitor.from_netlink(pyudev.Context())
monitor.filter_by('usb')
def kbd_event(action, device):
if action == 'add':
call(["setxkbmap", "carpalx"])
observer = pyudev.MonitorObserver(monitor, kbd_event)
observer.start()
setxkbmap carpalx
works if I type it in bash, but it doesn't change the layout in the above code. So I did this in bash:
setxkbmap carpalx
xmodmap -pke > carpalx2
changed the above call line to call(["xmodmap", "./carpalx2"])
and now the script works. I have the following problems:
- Why does xmodmap work in the code and setxkbmap doesn't, but both work in bash?
- Currently, kbd_event is called for every usb event and
call(["xmodmap", "./carpalx2"])
is run for every usb device I plug in. How can I further filter the events so the layout changes only when I insert a keyboard?
With my current code, the keyboard layout changes every time I plug in my mouse :)