keyboard.pl to HTML.

index -|- end

Generated: Sun Aug 21 11:11:08 2011 from keyboard.pl 2010/12/07 1.1 KB.

#!/usr/bin/perl -w
# NAME: keyboard.pl
# AIM: Simple test of polling the keyboard, ussing 'ReadKey'
# 07/12/2010 geoff mclane http://geoffair.net/mperl
use warnings;
use strict;
use Term::ReadKey;
#my @charlist = (); # try to collect list, but never happened...
sub got_keyboard {
    my ($rc) = shift;
#    if (@charlist) {
#        ${$rc} = pop @charlist;
#        return 1;
#    }
    if (defined (my $char = ReadKey(-1)) ) {
        ${$rc} = $char; # input was waiting and it was $char
        #while (defined (my $char = ReadKey(-1)) ) {
        #    push(@charlist,$char);
        #}
        return 1;
   }
    return 0;
}
sub forever_loop() {
    my ($char,$val,$msg);
    while (1) {
        if ( got_keyboard(\$char) ) {
            $val = ord($char);
            $msg = sprintf( "%02X", ord($char) );
            print "Got keyboard input hex [$msg]...\n";
            if ($val == 27) {
                print "Got ESC = exit...\n\n";
                last;
            }
        }
    }
}
print "Waiting for keyboard input... ESC key to quit...\n";
forever_loop();
exit(0);
# eof

index -|- top

checked by tidy  Valid HTML 4.01 Transitional