I want to check Lock keys (i.e Caps Lock, Num Lock, Scroll Lock etc.) state (On/Off) from command-line. How do I check state via terminal command?
Asked
Active
Viewed 9,869 times
3 Answers
15
simply run:
xset q
From man xset:
q The q option gives you information on the current settings.
In the top section of the output, you will find your information, looking like:
Keyboard Control:
auto repeat: on key click percent: 0 LED mask: 00000003
XKB indicators:
00: Caps Lock: on 01: Num Lock: on 02: Scroll Lock: off
03: Compose: off 04: Kana: off 05: Sleep: off
06: Suspend: off 07: Mute: off 08: Misc: off
09: Mail: off 10: Charging: off 11: Shift Lock: off
12: Group 2: off 13: Mouse Keys: off
You can use grep to get specific result as follows:
$ xset -q | grep Caps
00: Caps Lock: off 01: Num Lock: on 02: Scroll Lock: off
Pandya
- 34,843
- 42
- 126
- 186
Jacob Vlijm
- 82,471
- 12
- 195
- 299
-
I want to use caps state as an if conditional. (Specifically, I want to bind caps to control, but I only want to do that when caps isn't press, that leaves me IN PERMANENT ALL CAPS, WHICH IS NOT WHAT I WANTED! (phew). Suggestions? – Teodor Jan 05 '21 at 19:03
2
With xset you could use the following sed command:
xset -q | sed -n 's/^.*Caps Lock:\s*\(\S*\).*$/\1/p'
As an example, say you want to check if caps lock is enabled, and if so to disable it. For that you could do:
caps_lock_status=$(xset -q | sed -n 's/^.*Caps Lock:\s*\(\S*\).*$/\1/p')
if [ $caps_lock_status == "on" ]; then
echo "Caps lock on, turning off"
xdotool key Caps_Lock
else
echo "Caps lock already off"
fi
Mateo de Mayo
- 131
- 1
- 7
0
If you're not in X ("graphical mode"), but in a terminal:
$ /usr/bin/setleds
Current default flags: NumLock off CapsLock off ScrollLock off
Current flags: NumLock off CapsLock off ScrollLock off
Current leds: NumLock off CapsLock off ScrollLock off
Similarly, if you want to know the state of a terminal, but you're not in it (e.g. you came in through SSH):
setleds < /dev/tty1
You might need to be root, due to /dev/tty* permissions.
See the man page for more (the command can even set the leds, reading them is just a side effect).
Piskvor left the building
- 1,411
- 16
- 21