Listen For Key Press in .Net Console App

Listen for key press in .NET console app

Use Console.KeyAvailable so that you only call ReadKey when you know it won't block:

Console.WriteLine("Press ESC to stop");
do {
while (! Console.KeyAvailable) {
// Do something
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);

Reading Key Press in Console App

The reason for the behavior is simple:

You get inside the nested loop as long as there is no key pressed.
Inside you are waiting for a key and read it - So again no key is available.
even if you press escape, you are still inside the nested loop and never get out of it.

What you should of done is loop until you have a key available, then read it and check its value:

ConsoleKey key;
do
{
while (!Console.KeyAvailable)
{
// Do something, but don't read key here
}

// Key is available - read it
key = Console.ReadKey(true).Key;

if (key == ConsoleKey.NumPad1)
{
Console.WriteLine(ConsoleKey.NumPad1.ToString());
}
else if (key == ConsoleKey.NumPad2)
{
Console.WriteLine(ConsoleKey.NumPad1.ToString());
}

} while (key != ConsoleKey.Escape);

Listen for key press .NET console app does not stop running application

Your approach uses polling in a single thread, and it is not event driven.

The inner loop won't stop on key press. It will simply finish its work before the code checks for key press for the first time in the outer loops.

Move an abort condition like the following

 if(Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Escape)
{
break;
}

to the innermost loop.

There is another answer here which shows how to spawn a background thread to do the work whilst waiting for a keypress to exit on the main thread.

NET C# Console application not detecting key presses correctly

Not sure why do you need threads here.

  1. Run the while loop and check if there is KeyAvailable. Only if the key is available (a user pressed something), use the ReadKey function that blocks the thread.

  2. Provide a key that breaks the loop (ESC in this case).

     static void Main(string[] args)
    {
    Console.WriteLine("Press ESC to quit.");

    ConsoleKey key;
    do
    {
    while (!Console.KeyAvailable)
    {
    // Do something
    }

    key = Console.ReadKey(true).Key;

    if(key == ConsoleKey.Escape)
    {
    break;
    }
    else if (key == ConsoleKey.F12 || key == ConsoleKey.F11)
    {
    Console.WriteLine("F12 or F11 pressed");
    }

    } while (true);
    }

Detect key combination being pressed in C# console app

If you capture the ConsoleKeyInfo, you will get additional information, including the Modifiers keys. You can query this to see if the Control key was pressed:

ConsoleKey key;

do
{
while (!Console.KeyAvailable)
{
// Do something, but don't read key here
}

// Key is available - read it
var keyInfo = Console.ReadKey(true);
key = keyInfo.Key;

if ((keyInfo.Modifiers & ConsoleModifiers.Control) != 0)
{
Console.Write("Ctrl + ");
}
if (key == ConsoleKey.NumPad1)
{
Console.WriteLine(ConsoleKey.NumPad1.ToString());
}
else if (key == ConsoleKey.NumPad2)
{
Console.WriteLine(ConsoleKey.NumPad2.ToString());
}

} while (key != ConsoleKey.Escape);

Can i make a loop and listen for a key press and get this key value to a variable in C# Console Application without stop the loop

If you want the loop to continue, take KeyAvailable out of the while condition.

while (true)
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Escape) break;
}
//Do other stuff
}

How to handle key press event in console application

For console application you can do this, the do while loop runs untill you press x

public class Program
{
public static void Main()
{

ConsoleKeyInfo keyinfo;
do
{
keyinfo = Console.ReadKey();
Console.WriteLine(keyinfo.Key + " was pressed");
}
while (keyinfo.Key != ConsoleKey.X);
}
}

This will only work if your console application has focus. If you want to gather system wide key press events you can use windows hooks



Related Topics



Leave a reply



Submit