I have an external keyboard that I use with my Ubuntu 17.10 desktop and laptop at home, and the same species of keyboard on my RHEL 6.8 machines at work. But on my Ubuntu 17.10 laptop, I use the built-in keyboard most of the time, but other times I need to connect the previously mentioned external keyboard to it. So, I need the behavior of the capslock and control keys to work the same for both built-in and external keyboards.
This external keyboard has its customized firmware that, among many other things, swaps the left capslock and left control keys.
The behavior I am looking for is this:
- When the external keyboard is unplugged, swap the caps lock and control keys, since the built-in keyboard is in use.
- When I connect the external keyboard, I want to stop swapping the capslock and control keys because the keyboard does this already.
- I want this behavior under all of the following conditions:
- When I login using Gnome under Ubuntu 17.10
- When I login using Gnome under RHEL 6.8
- When I use the non-X terminal (console).
- I want to do this all from scripts I can control from my own home directory, and not have to install special packages under the root user as I need for it to work under both RHEL and Ubuntu (the latter I can control, but not the former, in terms of package installation and configuration).
What I have now is mostly working: I have a script run from Startup Applications that detects the keyboard and runs
xmodmap. But it is a hack because something in Gnome or Xlib or Xorg or whatever is also fiddling with the keyboard's capslock and control key behavior, such that I have to have a
sleep 5 right before the call to
xmodmap to give Gnome/whatever time to finish its "fiddling" so that my call to
xmodmap "wins". This is also a hack because it doesn't deal with unplugging and plugging in the keyboard. It should just do this based upon which type of keyboard is connected, not based upon logging in or out, locking or unlocking, or only at Linux startup. Also I had to have some setup that detects lock and unlocking of the desktop as well as then Gnome/whatever tries again to reset the keyboard upon unlock.
Is this possible to do?