I have a file called ~/.speedswapper which contains the following:
! Swap caps lock and escape
remove Lock = Caps_Lock
keysym Escape = Caps_Lock
keysym Caps_Lock = Escape
add Lock = Caps_Lock
…when I run xmodmap ~/.speedswapper
, this switches the esc and Caps Lock keys. I have this line in my ~/.profile, so that whenever I log in these keys are switched.
However, if I plug in an external USB keyboard, this setting doesn't seem to persist. My laptop's keyboard works perfectly well, but I have to run the command again in order for the external keyboard to switch the two keys. Fortunately, doing so doesn't seem to affect my laptop's keyboard — they seem to synchronise.
One solution would be to find some way to run that xmodmap command whenever an external keyboard is plugged in, but I'd be open to another keyswapping solution, if it would be more robust. OS is Ubuntu 13.04.
Given justbrowsing's comment, it looks like this can be achieved by writing a udev rule — I think I have to use the RUN option, and that it should be triggered on /dev/hidraw0 or /dev/hidraw1, which are the devices that appear when I plug my keyboard in… but I'm still trying to wrap my head around the concept, so I'd appreciate any help from someone who knows what they're doing with udev.
Best Answer
Since you have this command in your
~/.profile
, it will only be executed once, when you log in. One, not very elegant, solution would be to place the command in your~/.bashrc
instead so it will be run every time you open a terminal.A better solution would be to define a
udev
rule that will executexmodmap
when your USB keyboard is inserted (I am using the values returned for my USB keyboard, you will need to edit this solution to fit yours):Get the details of your USB keyboard. Run this command with the keyboard plugged in:
To write
udev
rules, you need to know what they should match against. You can get that by searching the output ofudevadm info --export-db
for theevent6
entry, or just parse it directly like so:That returns several lines of text including these which we will use:
udev
gets complicated when using X programs likexmodmap
, I couldn't even get it to work when exporting$DISPLAY
and$XAUTHORITY
. Anyway, I couldn't get it to work with your method, so my solution useskeymap
instead. First, you will need to find out the key codes for Esc and Caps Lock, you can do this by runningand then pressing the relevant keys. On my system, this returns:
Create a new keymap rule containing these lines:
and save it as
/lib/udev/keymaps/speedswap
.Define a new
udev
rule. Create a file called/etc/udev/rules.d/95-speedswap.rules
and add these lines to it:That should do it, at least on my system, plugging in my external USB causes Esc to act like Caps Lock and vice versa.