How to Handle Key Press Event in Console Application

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

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);

C# console key press event

You should use ReadKey method (msdn):

Console.Write("Press Y to start the game.");

char theKey = Console.ReadKey().KeyChar;

if (theKey == 'y' || theKey == 'Y')
{
Console.Clear();
Console.Write("Hello");
Console.ReadKey();
}
else
Console.Write("Error");

The return value of ReadKey method is ConsoleKey enum so you can use it in the if condition:

Console.Write("Press Y to start the game.");

ConsoleKey consoleKey = Console.ReadKey().Key;

if (consoleKey == ConsoleKey.Y)
{
Console.Clear();
Console.Write("Hello");
Console.ReadKey();
}
else
Console.Write("Error");

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);

Detecting key presses in console

You can use

Console.ReadKey();

To read 1 key. You could then do something like this:

string key = Console.ReadKey().Key.ToString();
if(key.ToUpper() == "W")
Console.WriteLine("User typed 'W'!");
else
Console.WriteLine("User did not type 'W'");

Or:

if(key == "")
Console.WriteLine("User pressed enter!");
else
Console.WriteLine("User did not press enter.");

And if you do not care if the user types anything but presses enter after, you could just do:

// Some code here
Console.ReadLine();
// Code here will be run after they press enter

How to handle keypress events in a Qt console application?

Qt doesn't handle console events, it can just read \n-terminated lines from the console.

You need to use native APIs or other libraries (curses).

Deployment of C# console application for Keyboard Listener

Have you tried setting lParam to null? It's been a while since i've done key handling, so not sure if it's better to set to null or IntPtr.Zero. One of the two should work:

public static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && wParam == (IntPtr)WM_KEYDOWN)
{
Keys pressedKey = (Keys)Marshal.ReadInt32(lParam);

if (pressedKey == Keys.F11 || pressedKey == Keys.F12)
{
// Do something...

// Don't pass the key press on to the system
lParam = null;
}
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}


Related Topics



Leave a reply



Submit