I have never tried this. But your problem seems to be solvable with systemd .device unit files or udev rules. The script would be like @xircon’s, and one or both of these two systems would manage it. You’ll need to learn about them or reformulate the question in a new thread, hoping that someone chimes in who knows how to do it. I can’t investigate at this time.
Without these, I don’t think there is any other way to run the script manually, since you don’t have any other programmable hooks I know of available to you when you plugin a keyboard device. Doing it manually isn’t a big of a problem anyway. It’s not that you plug and unplug your external keyboard like a you were having sex with the USB slot. So, if you end up finding the above methods too complex, don’t feel so bad about running a script manually once or twice per day. Running manual scripts is a normal occurrence among system administrators.