Detecting keyboard state from shell

Under development: PCMCIA, wireless, etc.
Post Reply
Message
Author
jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

Detecting keyboard state from shell

#1 Post by jamesbond »

Barry discussed the needs here (second comment): http://bkhome.org/blog2/?viewDetailed=00238. So I went googling for a while, found the answer here: http://stackoverflow.com/questions/3649 ... e-in-linux and tidy it up a little bit, as attached.

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. If you hack the source you can easily check for 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 PS/2 keyboard) but most of the time you would specify /dev/input/event5 or something. Scan /proc/bus/input/devices, find a device whose handlers is "kbd", and you know which /dev/input/eventXXX you need to specify.
If you need to test multiple keyboards (PS/2, USB, etc) you will need to call kbstate multiple times, one for each devpath.

To compile, just use your favorite compiler like this: "gcc -o kbstate kbstate.c". I've got it compiled statically under 64-bit musl, 32-bit and 64-bit aboriginal cross compiler.

Enjoy.

cheers!

EDIT: add 32-bit and 64-bit static binaries for convenience. Should work in all puppies, 32 and 64-bit.
Attachments
kbstate.gz
64-bit static executable (with musl). Gunzip first.
(2.26 KiB) Downloaded 427 times
kbstate32.gz
32-bit static executable. Gunzip first.
(3.49 KiB) Downloaded 424 times
kbstate.c.gz
Source. Gunzip first.
(988 Bytes) Downloaded 435 times
Last edited by jamesbond on Tue 14 May 2013, 16:02, edited 1 time in total.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#2 Post by Flash »

Puppy already has xev, which may not be useful in the same way.

jamesbond
Posts: 3433
Joined: Mon 26 Feb 2007, 05:02
Location: The Blue Marble

#3 Post by jamesbond »

xev requires X, this one runs in console and is meant to be used in initrd.
Fatdog64 forum links: [url=http://murga-linux.com/puppy/viewtopic.php?t=117546]Latest version[/url] | [url=https://cutt.ly/ke8sn5H]Contributed packages[/url] | [url=https://cutt.ly/se8scrb]ISO builder[/url]

User avatar
Flash
Official Dog Handler
Posts: 13071
Joined: Wed 04 May 2005, 16:04
Location: Arizona USA

#4 Post by Flash »

Ah. Thank you for clearing that up. :)

Post Reply