overrider has asked for the wisdom of the Perl Monks concerning the following question:

Hello fellow Perl Monks, i am building a Program that is installed on a headless embedded pc (Soekris 4801). The Unit has only a USB Port, to which i want to connect a Keyboard. I would like my Program to be able to read whatever is entered in there, so i can process it. Of course, since there is no screen, and noone logged in using the USB attached Keyboard, you cant just ask for input using <STDIN>. I need to read directly from the Keyboard Device, but what i have further down will simply generate some garbled Output as soon i start typing away on the keyboard that i want to read from. Same as if one would do `cat /dev/yourkeyboarddevice` , its not working that way.

Can anyone please tell me how to read directly from a Keyboard Device? Thanks a lot and best regards!
#!/usr/bin/perl use warnings; use strict; use Term::ReadKey; # read a char directly from a keyboard via /dev/wskbd1 open(my $fh, '<', '/dev/wskbd1') or die "Couldn't open Keyboard\n"; ReadMode(5,$fh); my $key = ReadLine(0, $fh); print "You entered $key\n"; ReadMode('normal',$fh); close($fh);

Replies are listed 'Best First'.
Re: Reading directly from a keyboard device, not simply <STDIN>
by Tanktalus (Canon) on Dec 16, 2008 at 03:50 UTC

    Of course, your other problem is that "there is no screen" so your print statement isn't going to do anything, either.

    So, what type of operating system does this embedded PC have? It doesn't have any HID drivers for USB keyboards? I've not done any embedded programming, but I would have expected that any device large enough to have perl on it would have a libc that would take care of this, whether it's DOS-ish-based or unix-like or some such. And that, of course, would mean that "cat -" would work, which would mean that just using stdin would work. Of course, I assume you've already tried that (being the most naive approach), so that's why I'm wondering what the OS is.

      Hey, the print statement is for examples sake. I logged into this machine via ssh, so the print $key would print on my terminal. The Box is a Soekris 4801 running OpenBSD 4.4.
Re: Reading directly from a keyboard device, not simply <STDIN>
by poolpi (Hermit) on Dec 16, 2008 at 11:38 UTC

    • Is /dev/wskbd1 the good keyboard control device ? (dmesg)
    • Try to attach/detach the usb keyboard from the wscons display with:
      wsconscfg -k 1
      wsconscfg -dk 1


    hth,
    PooLpi

    'Ebry haffa hoe hab im tik a bush'. Jamaican proverb
Re: Reading directly from a keyboard device, not simply <STDIN>
by zentara (Cardinal) on Dec 16, 2008 at 14:49 UTC
    There are some techniques in y/n input in a captive interface

    Here is something I played with for reading raw mouse. It locks up the X server, but may show a way.

    #!/usr/bin/perl use strict; # WARNING: will probably lock up your X-server my $data; my $fd; my $buf; use POSIX; $fd = POSIX::open("/dev/mouse", &POSIX::O_RDONLY) || die ($!); while($data = POSIX::read($fd, $buf, 1)) { print "READ $data Bytes: " . join("", map(sprintf("%x",ord($_)), sp +lit(//, $buf))) . "\n"; }

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Reading directly from a keyboard device, not simply <STDIN>
by Sagacity (Monk) on Dec 16, 2008 at 16:21 UTC
    Hi Overrider,

    This article talks about your machine not having a keyboard interface, and how they got around it! Article Even though it was a different reason for using the keyboard, it seems that the KeySpan USB to Serial interface did the trick for them. Good Luck!

Re: Reading directly from a keyboard device, not simply <STDIN>
by f00li5h (Chaplain) on Dec 16, 2008 at 05:11 UTC

    Term::ReadKey perhaps?

    (I know it's not technically reading from the device, but it's close-ish)

    @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;

      Yes, Term::ReadKey is what i am trying to use, see my example Code above. I seem to be doing it wrong though, as everytime i type something on my (attached to embedded box)keyboard , it just reads garbled characters instead of printing the string i actually typed.

      what i really want is to read (or sth close to read) from the keyboard device in the same way i would read from a device attached to a normal serial port.

        The link was supposed to be a nudge toword the docs...

        Term::ReadKey doesn't mention that it needs a file handle to do it's reading, have you tried without opening the $fh yourself, and just letting it default to using the current TTY?

        As for your nobody-logged-in issue, would it not be possible to spawn your perl scrpt from /etc/inittab? Then you will have everything you need set up (without having to log a user in via getty)

        Also, have you managed to get the module to behave the way you want on a box with a display? (it'll make debugging easier atleast)

        @_=qw; ask f00li5h to appear and remain for a moment of pretend better than a lifetime;;s;;@_[map hex,split'',B204316D8C2A4516DE];;y/05/os/&print;