<--- Turn the page     (contents page)     Turn the page --->


Basic

Getting a Password Without Showing All




Have you ever wanted to get a password from the user, but didn't want the password printed to the screen as the user was entering it? How about if we create a small routine to get a password and print something else instead.

In this routine, all we will do is get a single char at a time. We will also check for the Backspace key, and the Esc Key. Other keys would be easy to catch with the outline of this routine.

If the user presses any key greater than the space bar (ascii 33 and up), then we just add this to the password string. If the user pressed the Backspace key and we are not at position one in the password, then we move back a char. If the user presses the Escape key, then we delete the old password and start over. When the user pressed the Enter key, we are done.

Again, the way this routine is set up, all you have to do is add another item to the SELECT CASE statement to add another "key catch".

I have not allowed spaces in the password. You could simply change the below 32 to 31 and now can allow spaces. ¥



Start% = 25
CPos% = 0
Pass$ = ""

CLS
LOCATE 12
PRINT "Please enter password:  ";

DO
  LOCATE 12, CPos% + Start%
  PRINT "_";
  Char$ = INPUT$(1)
  SELECT CASE LEFT$(Char$, 1)
    CASE IS = CHR$(8)
      IF CPos% > 0 THEN
        IF LEN(Pass$) > 1 THEN
          Pass$ = LEFT$(Pass$, LEN(Pass$) - 1)
        ELSE Pass$ = ""
        END IF
        LOCATE 12, CPos% + Start%
        PRINT " "
        CPos% = CPos% - 1
      END IF
    CASE IS = CHR$(13)
      EXIT DO
    CASE IS = CHR$(27)
      Pass$ = ""
      CPos% = 0
      LOCATE 12, CPos% + Start%
      PRINT SPACE$(25);
    CASE IS > CHR$(32)
      Pass$ = Pass$ + LEFT$(Char$, 1)
      LOCATE 12, CPos% + Start%
      PRINT "*";
      CPos% = CPos% + 1
  END SELECT
LOOP

PRINT "["; Pass$; "]"
END




<--- Turn the page     (contents page)     Turn the page --->

Page 10