Exemplo n.º 1
0
        /// <summary>
        /// Ask for password.
        /// </summary>
        /// <param name="reattempt"></param>
        /// <returns></returns>
        internal static bool CheckPass(bool reattempt = false)
        {
            bool retVal = true;

            int[] PrevCursor = Defs.CurrentCursor;

            Console.SetCursorPosition(Defs.CurrentCursor[(int)CURSOR_LOC.LEFT], Defs.CurrentCursor[(int)CURSOR_LOC.TOP]);
            Log.Verbose(0, UserName, ConsoleColor.Yellow, true);
            Log.Verbose("-= [ Password ]=- :  ", ConsoleColor.Green, false);
            General.SaveCursor();

            while (string.IsNullOrWhiteSpace(PassWord) && retVal)
            {
                //check if capslock is on.
                if (Console.CapsLock)
                {
                    Console.SetCursorPosition(0, Defs.CurrentCursor[(int)CURSOR_LOC.TOP] + 2);
                    Log.Verbose("## CapsLock was ON ##", ConsoleColor.Red);
                    //if this is a reattmpt, then stay out of a loop.
                    if (!reattempt)
                    {
                        Thread.Sleep(1000);
                        //turn capslock off (keydown)
                        keybd_event(VK_CAPSLOCK, 0x45, KEYEVENTF_EXTENDEDKEY, (UIntPtr)0);
                        //turn capslock off (keyup)
                        keybd_event(VK_CAPSLOCK, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, (UIntPtr)0);
                        Console.SetCursorPosition(0, Defs.CurrentCursor[(int)CURSOR_LOC.TOP] + 2);
                        Log.Verbose("## CapsLock has been turned OFF ##", ConsoleColor.Red);
                        Thread.Sleep(1000);
                        Console.SetCursorPosition(0, Defs.CurrentCursor[(int)CURSOR_LOC.TOP] + 2);
                        Log.Verbose("## CapsLock has been turned OFF ##", ConsoleColor.Black);  //eraser
                        //let them see, caps lock has been turned off
                        Console.SetCursorPosition(PrevCursor[(int)CURSOR_LOC.LEFT], PrevCursor[(int)CURSOR_LOC.TOP] - 1);
                        General.SaveCursor();
                        return(CheckPass(true));
                    }
                }

                bool catchBreak = false;
                do
                {
                    ConsoleKeyInfo key = Console.ReadKey(true);

                    switch (key.Key)
                    {
                    case ConsoleKey.Escape:
                        PassWord   = null;
                        retVal     = false;
                        catchBreak = true;
                        break;

                    case ConsoleKey.Enter:
                        catchBreak = true;
                        break;

                    case ConsoleKey.Backspace:
                        if (PassWord.Length > 0)
                        {
                            PassWord = PassWord.Substring(0, (PassWord.Length - 1));
                            Console.Write("\b \b");
                        }
                        break;

                    default:
                        PassWord += key.KeyChar;
                        Console.Write("*");
                        break;
                    }
                } while (!catchBreak);
            }

            if (string.IsNullOrWhiteSpace(PassWord))
            {
                retVal = false;
            }

            return(retVal);
        }