#!/usr/bin/perl -w
#
# inkey() reads a keystroke from STDIN, if there is any.
# It returns -1, if no input was found.
#
use strict;
my $char_code;
my ($rin, $rout) = ( ”, ”);
sub inkey
{
my ($timeout, $key) = ( 0, "");
my ($nfound, $time_left) = select( $rout = $rin, undef, undef, $timeout);
$key = getc( STDIN) if( $nfound);
#
# treat special keys. they begin with <escape>
#
if( chr( 27) eq $key)
{
$key = getc( STDIN);
$key = getc( STDIN);
$key = 274 if( $key eq chr(65)); # up
$key = 275 if( $key eq chr(66)); # down
$key = 276 if( $key eq chr(67)); # right
$key = 277 if( $key eq chr(68)); # left
}
else
{
$key = unpack( "C*", $key);
}
$key = -1 if( !defined( $key));
return $key;
}
#
# finally switch back to no-cbreak, echo
#
END
{
system "stty", '-cbreak';
system "stty", 'echo';
print "\n BYE\n";
}
#
# cbreak mode and no-echo
#
system "stty", 'cbreak';
system "stty", '-echo';
vec( $rin, fileno( STDIN), 1) = 1;
while(1)
{
$char_code = inkey();
print "you typed $char_code\n" if( $char_code > 0);
}