kbstate [Updated 2015-12-28]

A small Linux tool to detect keyboad state (=key-pressed state) from shell. Useful for detecting various key states, for example, during boot time.

Usage: kbstate
Returns: Names of the keys pressed at that time, e.g. lshift, rshift, etc.

kbstate will scan through all /dev/input/event* and print out list of shift keys being pressed. The supported keys are

lshift,lalt,lctrl,rshift,ralt,rctrl,capslock,numlock,scrolllock,lmeta,rmeta,compose
(corresponds to Left Shift/Alt/Ctrl and their Right variantas, Caps Lock, Num Lock, Scroll Lock, Left Meta (usually Left Windows key), Right Meta (usually Right Windows key), and Compose (Usually Windows-Menu key on your right).

If multiple keys are pressed at the time kbstate is run, it will print out all these keys, separated by spaces.

This works both in console and terminal inside X session.

Get the source and the 32-bit x86 static binary (which will run on 64-bit systems too).

kbstate [Older version]

A small tool to detect keyboad state (=key-pressed state) from shell. Useful for detecting various key states, for example, during boot time.

Usage: kbstate key [devpath]
Returns: 0 if key is pressed, 1 otherwise, 255 for errors.

key is either lshift, rshift, lalt, ralt, lctrl, or rctrl (that's left/right shifts, left/right alts, left/right controls) in English keyboard, not sure the equivalents in others. You can edit the source to add other keys too.

devpath is the path to the Linux's keyboard event device. The default value is /dev/input/by-path/platform-i8042-serio-0-event-kbd (that is, the built-in keyboard port) but most of the time you would need to specify /dev/input/event5 or something if you use USB keyboard. Scan /proc/bus/input/devices to find devices whose "handlers" is "kbd", on the same line is the "eventXXX" to be used for that device (accessed through /dev/input/eventXXX which you pass to kbstate). If you need to check multiple keyboards (PS/2, USB, etc) you will need to call kbstate multiple times, one for each devpath.

This tool originally came from here, I have just tidied up a little to make it more readable.

Get the source, 32-bit static binary and 64-bit static binary.

Originally posted here: Detecting keyboard state in shell.