Knowledge Base

Preserving for the future: Shell scripts, AoC, and more

X11 switching keyboard layouts

tl;dr

For myself:

sudo apt-get install fbxkb
setxkbmap -option grp:switch,grp:shifts_toggle 'us,us(dvorak)'

Add to ~/.fluxbox/startup:

fbxkb &

The story

I learned that Xorg (if not all X11 implementations) provide input method switching built-in! You can change keyboard layouts with:

setxkbmap -layout us -variant basic

To switch to dvorak:

setxkbmap -layout us -variant dvorak

While researching in preparation for writing a tray icon utility to show/switch input methods, I learned that you can simply specify multiple input methods, and complex options such as switching between the two:

setxkbmap -option grp:switch,grp:shifts_toggle,grp_led:scroll 'us(basic),us(dvorak)'

See xkeyboard-config(7) for a list of options.

And in my research, I also learned that I don't need to write my own mktrayicon-powered one: Multiple already exist!

Fbxkb was the closest to what I wanted visually, functionally, and source code-wise. Multiple sources of fbxkb source code exist, such as a few github places, the sourceforge (abandoned) upstream in svn which I don't use, and the Debian source tarball.

I did fork it to add some small steps I wanted:

  • Display menu on left click, and increment input method on middle click
  • Enable rotating input methods on scrollwheel scrolling action
  • Add variant support, e.g., dvorak
  • Add tooltip

Research links

  1. https://fbxkb.sourceforge.net/faq.html
  2. https://unix.stackexchange.com/questions/22903/how-to-re-enable-the-caps-lock-key
  3. https://superuser.com/questions/1194468/invert-colors-with-imagemagick/1376630#1376630

Comments