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 1.3k times
3 Answers
19
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
- 37,289
Jacob Vlijm
- 85,475
4
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
- 161
- 1
- 8
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).