5 years ago I wrote inputplug, a tiny daemon which connects to your X server and monitors its input devices, running an external command each time a device is connected or disconnected.
I have used a custom keyboard layout and a fairly non-standard settings for my pointing devices since 2012 …