Password prompt routine

new topic     » topic index » view thread      » older message » newer message

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 smile

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.


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
        printf(1, "#", {})

        if (lenpw = 1) then
          pw = ""
        else
          pw = pw[2..lenpw]
        end if
      end if
    end if

    if ( (key >= 32) and (key <= 126) )
    then
      printf(1, "*", {})
      pw = append(pw, key)
    end if
  end while

  printf(1, "\n", {})

  return(pw)

end function


End of message.

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu