Page 1 of 1

Console App: hiding password input text??

Posted: Thu Nov 13, 2003 8:19 am
by Paul Dwyer
I have an ODBC consol application and it asks a user for a username and password. When the user types their password to be caught by the input() statement it's visible on screen but I'd like to have it appear as *****

How can I do this?

The only way of hiding it I can think of is setting the font colour to the background colour so it's invisible... not quite what I want though

Thanks

Posted: Thu Nov 13, 2003 8:47 am
by LarsG
Perhaps catch each keypress in a loop, then adding it to a string, and displaying it as *'s?!?

-Lars

Posted: Fri Nov 14, 2003 4:57 pm
by talun
Paul, is this rusty routines useful for you?

Code: Select all

; masked password input
; not for extended chars
If OpenConsole() 
  PrintN("Password please: (return to exit)")
  Passwd$ = ""
  Repeat 
    xkey$ = Inkey() 
    If xkey$ <> "" 
      If Asc(xkey$)>=32 And Asc(xkey$) < 128 
        Passwd$ = Passwd$ + Left(xkey$, 1)
        Print("*")
      EndIf
    EndIf  
  Until Left(xkey$, 1) = Chr(13)      
  PrintN("")
  PrintN(Passwd$)
  t$=Input()
  CloseConsole() 
EndIf 
End 
bye Sergio

Posted: Mon Nov 17, 2003 2:17 am
by Paul Dwyer
Thanks!

That sort of points me in the right direction. I want people to be able to backspace so I'll have to catch that.

Is there a way to catch the current location of the cursor on the screen so I can decriment it and move it back a place. I know I can set it's location but if I don't know where it is then I can't move it in relation to that...

Posted: Mon Nov 17, 2003 9:30 am
by plouf
whats wrong with #PB_String_Password in StringGadget :?:

Posted: Mon Nov 17, 2003 9:52 am
by Seldon
To know the current position, you can use a variable counter. Each time a letter is added you inc 1 to such variable, so you know where your cursor is. :P

Anyway, just for knowlegde, you can use the Win-API to know the current cursor position, look at this code (you can do a PB version in 3 seconds):

Code: Select all

HANDLE  hStdOut;
CONSOLE_SCREEN_BUFFER_INFO  csbi;

hStdOut=GetStdHandle(STD_OUTPUT_HANDLE);
GetConsoleScreenBufferInfo(hStdOut,&csbi);
return csbi.dwCursorPosition.Y+1;
Basically the CONSOLE_SCREEN_BUFFER_INFO structure contains the X;Y values of your cursor:

x=csbi.dwCursorPosition.X
y=csbi.dwCursorPosition.Y

Though I'd suggest you to use a simple counter.

Posted: Mon Nov 17, 2003 9:54 am
by Paul Dwyer
Thanks Seldon!

Plouf, Because it's a consol application not a windows application