Solved

Erasing characters using BackSpace

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

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

Suggested Solutions

Title # Comments Views Activity
Perl script to parse xml file 2 142
Perl output collect 7 70
Vb script to unzip a files and rename the files 12 130
Perl File::Find alternative 1 93
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…
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.…
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…

739 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