Results 1 to 3 of 3

Thread: Hiding console input

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2001
    Location
    USA
    Posts
    25

    Hiding console input

    I'm working on a console application where you have to enter in a username and password for a database and I was hoping to make it so when the user enters their password in, it isn't visible in the console window.

    I think I can figure out how to do it but the problem is I'm not sure how to capture each and every individual key. So then if I could do that, if the key was something other than enter, it would add it to a string and then write back: \b\0\b to erase it.

    So my question is, how can I capture each key?

  2. #2
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    Code:
    using System;
    using System.Text;
    
    class a
    {
    	Console.Write("Input a password: ");
    	StringBuilder sb = new StringBuilder();
    	while (true)
    	{
    
    		ConsoleKeyInfo cki = Console.ReadKey(true);
    		if (cki.Key == ConsoleKey.Enter)
    		{
    			Console.WriteLine();
    			break;
    		}
    		
    		if (cki.Key == ConsoleKey.BackSpace)
    		{
    			if (sb.Length > 0)
    			{
    				Console.Write("\b\0\b");
    				sb.Length--;
    			}
    			
    			continue;
    		}
    		
    		Console.Write('*');
    		sb.Append(cki.KeyChar);
    	}
    
    	Console.WriteLine("Your password is: {0}", sb.ToString());
    }
    that may help you.

  3. #3
    Frenzied Member
    Join Date
    Aug 2000
    Location
    Birmingham, AL
    Posts
    1,276
    I've seen that code snippet floating around the WWW but I think you need Whidbey to use it...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width