Keylock, Numlock and Layout indicator

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. :grin:

4 Likes

Nice work-around. :+1:

For numlock and capslock I found Indicator-Keylock, which is available from the aur, provides a nice tray resident noifier and also give on screen notifications.
Unlike your solution however, keyboard layout is not included.

1 Like

Maybe also worth a look:

For keys:

napcok@mabox ~ $ xset -q |grep Lock
    00: Caps Lock:   off    01: Num Lock:    on     02: Scroll Lock: off
    09: Mail:        off    10: Charging:    off    11: Shift Lock:  off


Keyboard layout:
https://man.archlinux.org/man/core/systemd/localectl.1.en


Maybe it is possible to create tint2 executor for that…
Executor can present Icons, text (with pango markup), also it is possible to bind actions or dynamic menu to them.

Tint2 executor docs

1 Like

If you are talking about Tea Barnes’s Indicator, it’s my all time favorite but the last time I tried was throwing building errors. That was the reason I came up with this solution instead.

1 Like

I had no problem installing and building it today.
Before I replied to your post I removed and re-installed to make sure it was ok. all went well

1 Like

I made a quick test with tint2conf and it works. Still couldn’t find the parameter to fix the Executor horizontal space in a way to fit both texts and avoid the automatic resizing.

Maybe something like that (quick and dirty):

#!/bin/bash

read CAPS NUM <<< $(xset -q |grep 00: |awk '{print $4,$8}')

[[ $CAPS = on ]] && CAPS_STATE="<span foreground='red'>C</span>" || CAPS_STATE="C"
[[ $NUM = on ]] && NUM_STATE="<span foreground='red'>N</span>" || NUM_STATE="N"

echo "$CAPS_STATE $NUM_STATE"

It always has the same width.
It works for me … but fun fact … I just realized that all my life I didn’t care about the states of these keys, and the best CAPS LOCK indicator in my case is … start typing :wink: