I\'m just not sure how I can mask with the addition of arrays. I would also like the masking system to recognize backspace not as a letter.
Module Module1
Sub M
This post has a good working example for you. It has masking with backspace and redo support.
I've done this once before, but instead of masking the password with, say an asterisk, I just left it blank (like entering a password in Linux). The latter is pretty straightforward:
Dim keyInfo as ConsoleKeyInfo = Console.ReadKey(True)
Dim enteredPassword as String = ""
' Read each entered character until user presses Enter.
While keyInfo.Key <> ConsoleKey.Enter
If keyInfo.Key = ConsoleKey.Backspace AndAlso enteredPassword.Length > 0 Then
enteredPassword = enteredPassword.Substring(0, enteredPassword.Length - 1)
Else
password &= keyInfo.KeyChar
End If
' Read next entered character
keyInfo = Console.ReadKey(True)
End While
To actually mask the password input, use the same idea, but after each character is entered and parsed, add a Console.Write("*"c). This get's a bit tricky with backspace, and the only way I know to simulate it would be to do:
Console.Write(ControlChars.Back)' move cursor back one column
Console.Write(" "c) ' clear the asterisk
Console.Write(ControlChars.Back)' move cursor back again to allow writing.
It's a bit prettier in C# in my opinion (and works using '\b' instead of ControlChars.Back) so your results may vary.
Also if there's an easier way to do this, I'd love to know, as this seems like reinventing the wheel for a fairly simple task.