Welcome alexx_sh,
I saw your post earlier, and I waited for some more knowledgeable Monk to tell you how to use curses. I haven't had too much luck using Perl curses, but I do have an editor I wrote several years ago that required handling function keys, so this code may be overkill for what you need. This works on AIX and most *nix systems, so test, test, test...
BEGIN
{
use POSIX qw(:termios_h);
use sigtrap qw( INT ); $Signal = 0;
our $config = qx/stty -g/;
my ($term, $oterm, $echo, $noecho, $fd_stdin, $iterm, $raw, $allra
+w);
$fd_stdin = fileno(STDIN);
$term = POSIX::Termios->new();
$term->getattr($fd_stdin);
$oterm = $term->getlflag();
$echo = ECHO | ECHOK | ICANON ;
$noecho = $oterm & ~$echo;
$iterm = $term->getiflag();
$raw = ICRNL;
$allraw = $iterm & ~$raw;
$SIG{INT} = \&TST_INT;
sub TST_INT
{ our $Signal++;
return;
}
sub cbreak
{ my $max = shift;
$term->setlflag($noecho);
$term->setiflag($allraw);
$term->setcc(VMIN, 0);
$term->setcc(VTIME, $max);
$term->setattr($fd_stdin, TCSANOW);
}
sub cooked
{ $term->setlflag($oterm);
$term->setiflag($iterm);
$term->setcc(VTIME, 1);
$term->setattr($fd_stdin, TCSANOW);
}
sub getkey
{ my $tm = shift; $key = ""; $chr = "";
cbreak($tm);
sysread(STDIN, $chr, 1);
$term->setcc(VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
GETLOOP:
#$# Need to test for multiple character Carr Return
if ( $er == 22 ) { return(""); }
if ( $chr ne "" )
{ $key .= $chr;
if ( ( length($key) == 2 )&&( ord($key) < 32 ) )
{ if ( substr($key,0,1) eq substr($key,1,1) ) { chop($
+key); }
}
if ( ( length($key) > 1 )&&( exists $CRTinput{$key} ) )
{ if ( $Debug >= 3 ) { PEMSG(3," GETKEY1:\t|$key|"
+); }
return $key;
}
if ( $chr ne $TERMCR ) { sysread(STDIN, $chr, 1); goto
+GETLOOP; }
}
if ( ( $Debug >= 3 )&&( length($key) > 1 ) ) { PEMSG(3," G
+ETKEY2:\t|$key|"); }
# cooked();
return $key;
}
sub clrkey
{ my $char = "";
$term->setlflag($noecho);
$term->setiflag($allraw);
$term->setcc(VMIN, 0);
$term->setcc(VTIME, 0);
$term->setattr($fd_stdin, TCSANOW);
sysread(STDIN, $char, 64);
}
}
END
{ cooked();
qx/stty $config/; print "\n\n";
$|=1;
}
Using the code in the BEGIN and END blocks allows the terminal to be reset to how we found at exit.
You get the character from the console/terminal/XTerminal by calling 'getkey();'
like this:
$key = getkey(25);
Hope this helps!
Good Luck!
"Well done is better than well said." - Benjamin Franklin
|