I’m fairly new to Openbox and one of things I found missing so far is the Caps Lock indicator (my laptop, for example, has no led in that key). Still haven’t figured out if this is available out of the box for tint2 or how to include it in the bar. In the meantime implemented -and improved- the same solution (script) I already posted in Manjaro’s forum.
First thing to keep in mind is that Openbox, also, does not includes a Keyboard Layout indicator (if there is please correct me). In my case, for example, I frequently change between an Spanish Latinoamerican (latam) and north american (us) keyboard. The easiest way to activate the layout change is editing the startup file ~/.config/openbox/autostart and then adding this line at bottom:
setxkbmap -layout "latam,us" -option "grp:alt_shift_toggle" &
This sets Alt-Shift
keys to cyclically rotate those two layouts but the only way to tell it did is by writing something. Let’s fix that first.
setxkbmap is great but there is no way to safely isolate the current layout of your keyboard because, instead, it shows them all. The only way so far, not very portable but great for Arch based distros is installing the AUR package xkblayout-state:
yay -S xkblayout-state
Next, take your favorite text editor and copy/paste this Shell Script code, save it to some know folder and don’t forget to give execute permissions:
#!/bin/bash
read -r CurrentCapsStatus CurrentNumStatus <<< "$(awk '/Caps Lock/ {print $4" "$8}' <<< "$(xset q)")"
read -r CurrentKeyboardLayout <<< "$(xkblayout-state print "%s")"
while true; do
sleep 1
read -r CapsStatus NumStatus <<< "$(awk '/Caps Lock/ {print $4" "$8}' <<< "$(xset q)")"
if [ "$CapsStatus" != "$CurrentCapsStatus" ]; then
case $CapsStatus in
"on" ) NotifyIcon="changes-prevent";;
"off" ) NotifyIcon="changes-allow";;
esac
notify-send -t 1 -i "$NotifyIcon" "Caps Lock ""$CapsStatus"
CurrentCapsStatus="$CapsStatus"
fi
if [ "$NumStatus" != "$CurrentNumStatus" ]; then
case $NumStatus in
"on" ) NotifyIcon="changes-prevent";;
"off" ) NotifyIcon="changes-allow";;
esac
notify-send -t 1 -i "$NotifyIcon" "Num Lock ""$NumStatus"
CurrentNumStatus="$NumStatus"
fi
# For Mabox/Openbox
read -r KeyboardLayout <<< "$(xkblayout-state print "%s")"
if [ "$KeyboardLayout" != "$CurrentKeyboardLayout" ]; then
NotifyIcon="ibus-keyboard"
notify-send -t 1 -i "$NotifyIcon" "${KeyboardLayout^^}"
CurrentKeyboardLayout="$KeyboardLayout"
fi
done
So, if you saved the script in, say: /my/scripts/folder/my-keylock-indicator.sh then issue a:
chmod +x /my/scripts/folder/my-keylock-indicator.sh
A few notes about the script:
- I choose a yellow open/close padlock as icons (changes-allow and changes-prevent) but you are free to change them for whatever you like in /usr/share/icons folder.
- Idem with the layout icon: a simple keyboard image (ibus-keyboard)
- Save/copy the script in a folder with enough permissions to be accessible by the SO at boot time.
- There is a one second delay on purpose to avoid high CPU usage (between 1-5%) when executing awk continually.
Finally, let’s activate the script at system startup. Edit ~/.config/openbox/autostart again and include his line followed by & at the end, ie:
/my/scripts/folder/my-keylock-indicator.sh &
Reboot your system and that’s it, every time you press the Caps Lock
, Num Lock
or Alt-Shift
you will get a brief notification about its current state.
Enjoy.