I'm using Ubuntu 20.10 and python 2.7. I've got a bash script which overwrites default keyboard bindings and it always works when I execute it from terminal, here's the script.
#!/bin/bash
xkbcomp -w0 -I$HOME/.xkb $HOME/.xkb/keymap/mykbd $DISPLAY
I've also added it to my .bashrc
file so that it executes always when I open a new terminal.
The problem is that I have a standing desk and sometimes when I adjust the height the keyboard is disconnected and connected again and I have to open a new terminal again so that my custom keybindings are applied again.
The best solution that I came up with is to create a systemd python job that will execute the bash script when my keyboard is plugged in. When I run the python script by itself it very rarely works, I can see in syslog that the python script calls the bash script when the keyboard is disconnected and connected again and the keybindings are overwritten but the new keybindings don't work. Here's the python script
#!/usr/bin/python
import functools
import os.path
import pyudev
import subprocess
def main():
BASE_PATH = os.path.abspath(os.path.dirname(__file__))
path = functools.partial(os.path.join, BASE_PATH)
call = lambda x, *args: subprocess.call([path(x)] + list(args))
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
monitor.filter_by(subsystem='usb')
monitor.start()
for device in iter(monitor.poll, None):
name = 'Freestyle_Edge_Keyboard'
if device.get('ID_MODEL') == name:
subprocess.call(['sh', '/home/user/scripts/loadKeyboardOverwrites.sh'])
if __name__ == '__main__':
main()
When I start the systemd job I can see in syslog that the keys are overwritten but it doesn't work and the only thing in syslog that I find a bit suspicious is the message about xkb_variant & xkb_options requires a string value
and error about myswap
Mar 24 21:33:32 user callShOnKeyboardPlugin.py[1533582]: Error: Can't find file "myswap" for symbols include
Mar 24 21:33:32 user callShOnKeyboardPlugin.py[1533582]: Exiting
Mar 24 21:33:32 user callShOnKeyboardPlugin.py[1533582]: Abandoning symbols file "(null)"
Mar 24 21:17:01 user /usr/libexec/gdm-x-session[28481]: (**) Option "xkb_model" "pc105"
Mar 24 21:17:01 user /usr/libexec/gdm-x-session[28481]: (**) Option "xkb_layout" "sk"
Mar 24 21:17:01 user /usr/libexec/gdm-x-session[28481]: (WW) Option "xkb_variant" requires a string value
Mar 24 21:17:01 user /usr/libexec/gdm-x-session[28481]: (WW) Option "xkb_options" requires a string value
The myswap file is located in ~/.xkb/symbols/myswap
Here's the systemd job
[Unit]
Description=desc
[Service]
ExecStart=/home/user/scripts/callShOnKeyboardPlugin.py
Restart=always
Environment="PYTHONPATH=$PYTHONPATH:/home/user/.local/lib/python2.7/site-packages"
[Install]
WantedBy=multi-user.target
Does anyone have any idea where the issue could be please? Or is there any other better way how to do this? Any help would be greatly appreciated.
If there's something missing please let me know and I'll provide it.
Update
I handled it in a different way. I've set it up as a startup script and also I've added a bit of delay after when the keyboard was detected and that made it work consistently.