Solved

Erasing characters using BackSpace

Posted on 1998-10-29
2
1,897 Views
Last Modified: 2006-11-17
I'd like to perform a char-by-char login and password input. How can I display input properly when BackSpace key pressed? The current source is like the following, but it does not erase chars (I don't know how to do this). It should work in Unix, Win32 used for some testing only.

#!/usr/local/bin/perl
#
use Term::ReadKey;
use English;

my $s;

if ($OSNAME =~ /WIN32/i) {
    $ENTER = chr(13);
} else {
    $ENTER = chr(10);
}  

print "Password: ";
readstring($s, "*");
exit;

#**************************************
sub getchar {
    ReadMode 4;
    my $k;
    while(!defined ($k)) {
       $k = ReadKey(-1);
       next if (!defined ($k));
    }
    ReadMode 0;
    return $k;
}

#**************************************
sub readstring {
    my ($str, $passwordchar) = @_;
    my ($s) = "";
    my ($i_char) = "Empty";
    while ($i_char ne $ENTER) {

        $i_char = getchar();

        if (ord($i_char) == 8) {            # BackSpace
            $s = substr($s, 0, length($s)-1);
        } elsif ($i_char ne $ENTER) {
            print (defined $passwordchar ? $passwordchar : $i_char);
            $s .= $i_char;
        } elsif ($i_char eq $ENTER) {
        } else {
            print "Unknown character in line!\n";
        }
    }
    $_[0] = $s . "\n";
    return 0;      # normal completion
}

0
Comment
Question by:dda
2 Comments
 
LVL 5

Accepted Solution

by:
b2pi earned 100 total points
ID: 1205810
\b is the backspace char, but it doesn't erase.  So follow it with a space. Try this:

print "asdfasdf\r";
print "asdf";
print "\b \b";

print "\n\n";

Note that this will cause problems if with things like tabs, line feeds, et al.
0
 
LVL 4

Author Comment

by:dda
ID: 1205811
It works!. Thanks a lot.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question