Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Problems with Term::ReadKey

by uberdisco (Initiate)
on Sep 17, 2011 at 18:51 UTC ( [id://926567]=perlquestion: print w/replies, xml ) Need Help??

uberdisco has asked for the wisdom of the Perl Monks concerning the following question:

So my issue is Term::ReadKey is broken for me. When I use something like the attached code I see nothing until I hit enter. I have tried all the different options the module allows to no avail. The sample code is just that, a sample of code that doesn't work, I have simplified the code down to its bare essentials and tried that to no avail. Any ideas? Perhaps its my terminal?
#!/usr/bin/perl use strict; use warnings; use Term::ReadKey; my $key = 0; my $password = ""; print "\nPlease input your password: "; # Start reading the keys ReadMode(4); #Disable the control keys while(ord($key = ReadKey(0)) != 10) # This will continue until the Enter key is pressed (decimal value of +10) { # For all value of ord($key) see http://www.asciitable.com/ if(ord($key) == 127 || ord($key) == 8) { chop($password); #2 move the cursor back by one, print a blank character, move +the cursor back by one print "\b \b"; } elsif(ord($key) < 32) { # Do nothing with these control characters } else { $password = $password.$key; print "*(".ord($key).")"; } } ReadMode(0); #Reset the terminal once we are done print "\n\nYour super secret password is: $password\n";

Replies are listed 'Best First'.
Re: Problems with Term::ReadKey (output problem, not input)
by tye (Sage) on Sep 17, 2011 at 19:11 UTC

    Add $|= 1;. (perlvar)

    Update: You should also treat chr(13) the same as chr(10) as the raw mode you are putting things into means that the default translation to a "\n" upon input of a "\r" is disabled (at least in some environments). I had to type Ctrl-J to get it to end.

    - tye        

      Adding
      $|= 1;
      solved the problem. Thank you very much, this was driving me nuts! Cheers, uberdisco
Re: Problems with Term::ReadKey
by Khen1950fx (Canon) on Sep 18, 2011 at 08:22 UTC
    I had to Ctrl-J also. You need a way to get out if necessary. What if the user had second thoughts about putting his password out there, and wanted to quit?
    exit 0 if $key eq 'q';
    Here's what I tried:
    #!/usr/bin/perl -slw use strict; use Term::ReadKey; my $key = 0; my $password = ""; print "\nPlease input your password: "; ReadMode(4); while(ord($key = ReadKey(0)) != 10) { exit 0 if $key eq 'q'; if(ord($key) == 127 or ord($key) == 8) { chop($password); print "\b \b"; } elsif(ord($key) < 32) { (); } else { $password = $password . $key; print ord($key); } } ReadMode(0); print "\n\nYour super secret password is: $password";
    Update: If 'q' is a problem for you, substitute a fake password like 'quit', 'exit', 'bye', etc. for 'q';
Re: Problems with Term::ReadKey
by pvaldes (Chaplain) on Sep 18, 2011 at 13:38 UTC
    See nothing until I hit enter. Perhaps its my terminal?

    I think so, it runs smoothly in my system without a warning and I can see the chars before enter, then the program exit correctly. Same behaviour when running the script in a tty

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://926567]
Approved by keszler
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (7)
As of 2024-03-28 11:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found