Solved

Erasing characters using BackSpace

Posted on 1998-10-29
2
1,899 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: 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

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

696 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