#!/usr/bin/perl use warnings; use strict; my $str; my $buff; $| = 1; for (1..100) { my $got; $got = getone(); my $hx = unpack("H*",$got); # Look for "esc" character if ($hx eq '1b') { $str = $hx; } # Look for the 3 byte arrow sequence elsif ($str) { $str .= $hx; if ( length($str) == 6 ) { if ( lc($str) eq '1b5b41' ) { print "up arrow!\n"; } elsif ( lc($str) eq '1b5b42' ) { print "down arrow!\n"; } $str = undef; } } # If not enter key, add to buffer elsif (lc($hx) ne '0a') { print "$got"; $buff .= $hx; } else { print "\nline is: ". pack("H*", $buff)."\n"; $buff = undef; } } exit; # Camel 3rd ed, pp. 905-906 BEGIN { use POSIX qw(:termios_h); my ($term, $oterm, $echo, $noecho, $fd_stdin); $fd_stdin = fileno(STDIN); $term = POSIX::Termios->new(); $term->getattr($fd_stdin); $oterm = $term->getlflag(); $echo = ECHO | ECHOK | ICANON; $noecho = $oterm & ~$echo; sub cbreak { $term->setlflag($noecho); $term->setcc(VTIME, 1); $term->setattr($fd_stdin, TCSANOW); } sub cooked { $term->setlflag($oterm); $term->setcc(VTIME, 0); $term->setattr($fd_stdin, TCSANOW); } sub getone { my $key = ""; cbreak; sysread(STDIN, $key, 1); cooked(); return $key; } } END { cooked() }