in reply to Reading a password from the user
I wrote this ages ago for a few quick scripts. It's less than slick, for example it uses global variables, but I found it useful. The Win32::Console module this script uses should be installed with AS.
use strict; use warnings; use Win32::Console; #Stores handles to stdin/out of the console my $hSTDIN = new Win32::Console(STD_INPUT_HANDLE); my $hSTDOUT = new Win32::Console(STD_OUTPUT_HANDLE); print AskPassword('Password'), "\n"; exit; ##################################################################### sub AskPassword { #Ask the user to supply a password ##################################################################### my ($szPwd, $iEventType, $bKeyDown, $iKeyCode, $iAsciiCode) = ('', 0, 0, 0, 0); my $szPrompt = shift @_; #The question to ask if (defined $szPrompt) {print $szPrompt, ': '} EVENT: while () { #Wait for a console event ($iEventType, $bKeyDown, undef, $iKeyCode, undef, $iAsciiCode, + undef) = $hSTDIN->Input(); #Check event if (not defined $iEventType) { next EVENT } if ($iEventType != 1) { next EVENT } #Not a keyboard event if (not $bKeyDown) { next EVENT } #Not a key down event #Enter key pressed if ($iKeyCode == 13) { last EVENT } #Handle backspace or delete being pressed if ($iKeyCode == 8 or $iKeyCode == 46) { if (length $szPwd == 0) { next EVENT } #Remove offending character from screen and $szPwd MoveCursor(-1, 0, 'R'); print ' '; chop $szPwd; MoveCursor(-1, 0, 'R'); #re-position for next char next EVENT; } #Part of the password if ($iAsciiCode > 32) { print '*'; $szPwd .= chr($iAsciiCode); } } #end EVENT print "\n"; return $szPwd; } ##################################################################### sub MoveCursor # Moves cursor relative to the current positon # or to an absolute position ##################################################################### { my ($iX, $iY, $szMode) = @_; my ($iCurrX, $iCurrY); ($iCurrX, $iCurrY) = $hSTDOUT->Cursor(); if ($szMode eq 'R') { #relative $hSTDOUT->Cursor($iCurrX + $iX, $iCurrY + $iY); } if ($szMode eq 'A') { #absolute if (not defined $iX) {$iX = $iCurrX} if (not defined $iY) {$iY = $iCurrY} $hSTDOUT->Cursor($iX, $iY); } }
Regards,
Dom.
|
|---|