Running two codes at the same time

104 Views Asked by At

I want to read the password(Text) input while keeping the "CapsLock = true/false" readout up to date while the user is typing in Console application C#

User is Typing
Show Capslock is on/off

is it need using threading or not??

Thank for watching thank for answer and thank for help me guys.. ty

2

There are 2 best solutions below

1
Auditive On BEST ANSWER

That's weird, but possible.

First problem is that Console won't let you write something until user finish his input. So you won't be able to handle CapsLock press "on-the-fly" and notify user about its state change. Even from separate threads. But you can use MessageBox from PresentationCore.dll or System.Windows.Forms.dll and show it independently.

Second problem is that you can't properly handle CapsLock press as some sort of KeyPress event. So you need manually check and detect its state changes.

I've got things work with this example:

 using System;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 // ...

 private static string PromptUserPassword()
 {
     // Flag to stop CapsLock state checks after user input
     var passwordInputted = false;

     // Run CapsLock state checks loop somewhere in background
     Task.Run(async () =>
     {
         // "Remember" current CapsLock state
         var capsLockLastState = Console.CapsLock;

         while (!passwordInputted)
         {
             // When CapsLock state changed - notify about that with MessageBox and "remember" new state
             if (Console.CapsLock != capsLockLastState)
             {
                 capsLockLastState = Console.CapsLock;
                 MessageBox.Show($"CapsLock is turned {(Console.CapsLock ? "ON" : "OFF")}");
             }

             await Task.Delay(10);
         }
     });

     // Ask user password
     Console.WriteLine("Please, input your password:");
     var password = Console.ReadLine();
     // "Break" CapsLock state checks loop
     passwordInputted = true;

     return password;
 }

And from somewhere in code in need to call just var password = PromptUserPassword();

There is a lot of nuances with this code, such as you can just "ignore" shown MessageBox and keep switching CapsLock and then receive tonns of MessageBox'es.

That example provided just for familiarization. I highly recommend NOT to use this in real authorization purposes, and probably migrate into WPF or WindowsForms at least for proper implementations.

1
Yasim Yasin On

thanks to Auditive... the final answer :

using System;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var capsLockLastState = Console.CapsLock;
            capsLockLastState = Console.CapsLock;
            int origRow = Console.CursorTop; int origCol = Console.CursorLeft;
            Console.SetCursorPosition(55, 23);
            Console.ForegroundColor = ConsoleColor.Red;
            Console.WriteLine($"CapsLock is turned {(Console.CapsLock ? "ON/" : "OFF")}");
            Console.ForegroundColor = ConsoleColor.Gray;
            Console.SetCursorPosition(origCol, origRow);

            var password = PromptUserPassword();

        }
        private static string PromptUserPassword()
    {
        var passwordInputted = false;
        Task.Run(async () =>
        {
            var capsLockLastState = Console.CapsLock;

            while (!passwordInputted)
            {
                if (Console.CapsLock != capsLockLastState)
                {
                    capsLockLastState = Console.CapsLock;
                    int origRow = Console.CursorTop; int origCol = Console.CursorLeft;
                    Console.SetCursorPosition(55, 23);
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine($"CapsLock is turned {(Console.CapsLock ? "ON/" : "OFF")}");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    Console.SetCursorPosition(origCol, origRow);
                }
                await Task.Delay(10);
            }
        });
        Console.WriteLine("Please, input your password:");
        var password = Console.ReadLine();
        passwordInputted = true;
        return password;
    }
    }
}