I am trying to do a press Q to quit thing in the console window. I dont like my current implementation. Is there a way i can async or use a callback to get keys from the con
Taking from all the answers here, this is my version:
public class KeyHandler
{
public event EventHandler KeyEvent;
public void WaitForExit()
{
bool exit = false;
do
{
var key = Console.ReadKey(true); //blocks until key event
switch (key.Key)
{
case ConsoleKey.Q:
exit = true;
break;
case ConsoleKey.T:
// raise a custom event eg: Increase throttle
break;
}
}
while (!exit);
}
}
static void Main(string[] args)
{
var worker = new MyEventDrivenClassThatDoesCoolStuffByItself();
worker.Start();
var keyHandler = new KeyHandler();
keyHandler.KeyEvent+= keyHandler_KeyEvent; // modify properties of your worker
keyHandler.WaitForExit();
}