Update: 13 is the ord value of the enter key and 8 is the backspace, you will need to use 10 for your enter key on a unix OS. Windows consoles "steal" the delete and arrow keys so I can't catch them. You may be able to add them to handle rubouts, it just wouldn't do it on Win32, most folks use backspace to rubout so I didn't try too hard to find a way to bind to the delete key.#!/perl/bin/perl use Term::ReadKey; while(1){ ReadMode 3; #cbreak mode &menu; print "\tChoice: "; my $key = ReadKey(); print "$key"; &hello_world if $key == 1; &get_password if $key == 2; if( $key eq 'q' || $key eq 'Q'){ print "\nExiting"; exit(0); } } sub get_password{ binmode STDIN, ":raw"; ReadMode 3; my ($key, $val, $pass, $backstop); my @password; my $prompt = "\n\n\tPassword: "; system("cls"); # NOTE if you make a call to the system you have to +reset the ReadMode :) #After we make the system call... ReadMode is set back to normal or 0! +! ReadMode 3; print "$prompt"; while($key = ReadKey()){ $val = ord $key; push @password, $key unless $val == 8 || $val == 13; $backstop = @password; last if $val == 13; if($val == 8){ pop @password; if($backstop) {print "\b \b"; } } print "*" unless $val == 8; } ReadMode 0; binmode STDIN, ":crlf"; foreach(@password) { $pass .= $_; } print "\n\n\tYou entered: $pass\n\t(will return to main menu in 3 tick +s)"; sleep 3; system("cls"); } sub menu{ print <<MENU; [1] Hello World [2] Password Sub [Q] Exit MENU } sub hello_world{ system ("cls"); for(1..10){ print "\n\t\tHELLO WORLD \n"; } print "\n\t(will return to main menu in 3 ticks)"; sleep 3; system ("cls"); }
In reply to Re: ~ simple command line program ~
by JamesNC
in thread simple command line program
by primus
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |