in reply to Term::ReadKey problem!
I had a little fun playing with your program. I don't know if I solved the problem you're having or not. (Mostly because I was having so much fun, I forgot what the problem was while playing around.) The original version had a few quirks, but this one works better on my machine. Please give it a try and let me know what you think...
--roboticus#!/usr/bin/perl -w use strict; use warnings; use Term::ReadKey; startListening(); sub startListening { ReadMode(4); # RAW mode (we'll do all the work!) my $echofl=1; # 1=Normal, 0=Password mode my $echoPwChar='#'; # Password mode char my $word = ''; # Input accumulator my $prompt = "> "; print $prompt; while (1) { my $char = ReadKey(0); my $ord = ord($char); if ($ord == 3) { # "\003" (^C) : STOP! print "\n^C -- terminated!\n"; last; } elsif ($ord == 10 or $ord == 13) { # "\n" (^J), "\r" (^M) : Finish line enterPressed($word); $word = ""; print $prompt; } elsif ($ord == 5) { # "\005" (^E) : Toggle echo mode $echofl = 1-$echofl; } elsif ($ord==127 or $ord==8) { # "\177" (DEL), "\010" (^H) : Kill last char print chr(8),' ',chr(8) if length($word)>0; chop($word); } elsif ($ord == 27) { # "\033" (Esc) : Clear line print chr(8) x length($word) . ' ' x length($word) . "\r" . $prompt; $word=""; } else { #if ($ord != 0) { # Otherwise add to word & update display $word .= $char; $char = $echoPwChar if !$echofl; print $char; } } ReadMode(0); } sub enterPressed { my $word = shift; print "\n '" . $word . "'\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Term::ReadKey problem!
by Ace128 (Hermit) on Jun 15, 2006 at 14:42 UTC |