In article <[EMAIL PROTECTED]>,
Stephen Hardisty wrote:

> Afternoon,
> you could use Term::ReadKey, for example:
> 
> use Term::ReadKey;
> 
> # if you hit ctrl-c you still want to return to 'normal'
> $SIG{INT} = sub{ReadMode('normal')};
> 
> ReadMode('cbreak');
> 
> # 'ord' returns the ASCII value of a character
> # and 27 is the value of escape
> if(ord(ReadKey(0)) == 27)
> {
> print "Escape";
> }
> else
> {
> print "Something else";
> }
[...]

I ran into this but was also accepting up and down arrow input which also
begin with esc. With ReadKey you can also check to see if there is
additional input waiting in the buffer. This is what I came up with just by
winging it (improvements, suggestions to do this better are welcome):

while (1) {                                     # get line of input

    $char = ReadKey(0);
    last if ord($char) == 10;                   # return

    if (ord($char) == 27) {                     # esc
        print $clear_line, $prompt;

        # check for other chars. after 'esc'; if not exit sub

        unless (defined (my $key  = ReadKey(.01))) {
            ReadMode('normal');
            return 'esc' if $CONF{$type};       # if called by configure()
            return '';                          # else return ''
        }

        $esc_flag = 1;
    }

    if ($esc_flag) {

        no warnings;

        if ($char eq "A") {                     # up arrow
            # do something
        } elsif ($char eq "B") {                # down arrow
            # do something
        }

    } elsif (ord($char) == 127) {               # backspace
        pop @line;
        print $clear_line, $prompt;
        print @line;

    } else {
        $history_ptr = 0;
        print $char;
        push @line, $char;
    }

}
    ReadMode('normal');


I suspect that Term::ReadLine is the easier way to go but have not looked at
it too closely (yet).


-- 
Kevin Pfeiffer
International University Bremen
A: Because it messes up the order in which people normally read text.
Q: Why is top-posting such a bad thing?

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to