Skip to main content

Different keyboards and udev

I have a hobby about mechanical keyboards (nothing serious just yet). One of the issues I had recently was about me modifying some layout options; specifically I swapped the esc with the Caps Lock key.

This is pretty straightforward thanks to gnome-tweaks:

  • Open gnome-tweaks
  • Click on Keyboard & Mouse
  • Click on the Additional Layout Options button
  • Click the Caps Lock behavior drop-down option
  • Select Swap Esc and Caps Lock

From here all is OK, but then I entered the programmable mechanical keyboards. In summary, I currently own 2 of these:

  1. Vortex - Cypher 65% (double space-bar)
  2. Ducky - One 2 SF

The thing here is that the first one allows me to directly program the Caps Lock key as an esc key, so there’s no more need to change any configuration within the Desktop/OS. However, the Ducky keyboard does NOT allow to reprogram the Caps Lock key, what a bummer!

All in all, this is the situation:

  • When I plug the Vortex Cypher keyboard, I don’t need to setup any layout option
  • When I plug the Ducky one 2 SF (or when I use a laptop), I DO need to setup the Swap Esc and Caps Lock layout option

What could I do to get this done, other than manually changing the option every time? A fancy udev rule!

Udev

To keep it short, I won’t say what udev is or how it’s supposed to work, but I’ll note that I can solve my kind of problem previously described by defining a udev rule that changes these options for me whenever a device is plugged/unplugged.

What I want is:

  • Enable the Swap Esc and Caps Lock layout by default
  • Disable/unset the previous layout option whenever my Vortex Cypher keyboard is plugged

I found that the idVendor and idProduct were 04d9 and 0282, respectively. Additionally, I can change these configurations by using gsettings instead of gnome-tweaks. (So it’s programmable, not GUI/Click-based)

This is the udev rule I ended up with:

# reset layout options when vortex cypher keyboard is plugged
ACTION=="add", ATTRS{idVendor}=="04d9", ATTRS{idProduct}=="0282" \
, ENV{DISPLAY}=":0" \
, ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority" \
, ENV{DBUS_SESSION_BUS_ADDRESS}="unix:path=/run/user/1000/bus" \
, RUN+="/usr/bin/sudo -u mrkz /usr/bin/gsettings reset org.gnome.desktop.input-sources xkb-options"

# set layout options when vortex cypher keyboard is unplugged
ACTION=="remove", ATTRS{idVendor}=="04d9", ATTRS{idProduct}=="0282" \
, ENV{DISPLAY}=":0" \
, ENV{XAUTHORITY}="/run/user/1000/gdm/Xauthority" \
, ENV{DBUS_SESSION_BUS_ADDRESS}="unix:path=/run/user/1000/bus" \
, RUN+="/usr/bin/sudo -u mrkz /usr/bin/gsettings set org.gnome.desktop.input-sources xkb-options \"['caps:swapescape']\""

At the end it will need a bit more work so it’s more generic (and I can add it as part of my dotfiles repository), but that will be for another time.