Solved

Erasing characters using BackSpace

Posted on 1998-10-29
2
1,892 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now