How can I check Caps Lock status without using so much CPU?

November 13, 2026 4 answers
Asked by almost__but_not_quite

I want a sound when Caps Lock turns on or off in CachyOS with Hyprland. My Bash script checks the keyboard LED brightness every 20 milliseconds, but CPU usage spikes. Is there a better way to detect the change?

Answers

_retry_at_lunchtime

That loop launches cat through command substitution 50 times a second. The sleep keeps it from being a completely tight loop, but repeatedly starting processes is still unnecessary work.

If polling is good enough, try reading the brightness file with Bash's built-in read instead of cat, and increase the interval to something like 0.2 seconds. Check that the LED path exists and is readable before entering the loop. You may need to choose the right path if there is more than one matching LED.

_doodle_while_waiting_:

A slower interval also means the sound can be delayed by up to roughly that interval. It's a tradeoff rather than a perfect fix.

Reply to this answer
rOundTripToNowhere

I'd look at an event-driven option first. A Hyprland key binding could trigger a script when Caps Lock is pressed, rather than checking a file continuously.

The tricky bit is knowing the resulting state, not just noticing a keypress. Make sure the script reads the state after it changes, and test whether your binding still lets Caps Lock work normally.

Reply to this answer
YEP.just.checked

Another route is to listen to keyboard input events. Linux input devices can report LED events, so a program could wait for a Caps Lock LED update and play the matching sound without a polling loop.

That is more involved than a shell loop. You need access to the appropriate input device and need to handle devices appearing or disappearing. Parsing the raw event structures directly also depends on their layout, so a proper input-event library is safer than assuming a fixed byte size.

Reply to this answer
twelve_204_variations

One caveat: keyboard LED brightness isn't necessarily the same thing as the logical Caps Lock state. The LED can be controlled independently, and multiple keyboards can leave you reading the wrong device.

Decide whether you want the sound to follow a particular keyboard's indicator or the state used for typing. That choice affects which source you should monitor, and what should happen when no suitable keyboard is available.

Reply to this answer

Add your answer

Answers are reviewed before publication.

Related Discussions