Re: Password prompt routine
- Posted by freeplay at mailandnews.com Aug 18, 2001
- 454 views
Hello again, Thanks for all the suggestions. In the end I worked out that a: printf(1, "\r", {}) puts the cursor back to the beginnning of the line. So to erase the last character printed I would need to goto the beginning of the line, print out the line again but have the last character in the line be a space (to erase) and then goto the beginning of the line again and reprint the line minus the last character. You can detect a bit of cursor flicker but it's good enough for me Here is the revised routine: function getpw(sequence prompt) sequence pw integer key integer lenpw printf(1, "%s", {prompt}) pw = "" while 1 do lenpw = length(pw) key = wait_key() if ( (key = 10) or (key = 13) ) then if lenpw > 0 then exit end if end if if ( (key = 8) or (key = 127) ) then if (lenpw > 0) then if (lenpw = 1) then pw = "" else pw = pw[2..lenpw] end if lenpw = lenpw - 1 printf(1, "\r%s", {prompt}) for i = 1 to lenpw do printf(1, "*", {}) end for printf(1, " ", {}) printf(1, "\r%s", {prompt}) for i = 1 to lenpw do printf(1, "*", {}) end for end if end if if ( (key >= 32) and (key <= 126) ) then if (lenpw < MAXPWLENGTH) then printf(1, "*", {}) pw = append(pw, key) end if end if end while printf(1, "\n", {}) return(pw) end function At 00:00 18/08/01 +0100, I wrote: > >Hello again folks! > >Ok I've got a routine I've called "getpw". You give it a prompt string, it >displays this prompt and then accepts a line of input which is terminated >when you press return. The routine returns the typed line. When you type >a character it prints a single '*' character so you know you've typed the >character but has the nice effect that what you type is hidden from prying >eyes. Hence GET PassWord > >I detect the backspace key (ASCII code 8) and deal with it accordingly >(i.e. throw away the last character typed). I was hoping to print a >"backspace, space, backspace" sequence to rub out the last '*' character >but can't get this to work. Instead I'm currently printing a '#' character >to indicate that the last character has been "erased". If someone can help >me achieve the "backspace, space, backspace" effect I'm after I'd be very >grateful. > >Regards. > >FP. <snipped>