#!/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
