Checking for a keypress in console.
-
I want to have a loop that checks if a key has been pressed during that time. The loop has a delay of Time.Sleep(25); So the loop starts, check if a key is down then continues with the code, the time delay hits then it repeats. The problem is I can't use ReadKey Read or ReadLine because these all stop the "flow" of the code causing it to physically halt, I want to code(loop) to continue if a key is pressed or not. And System.Windows.Forms; isnt an option as far as I know. Thanks guys.
-
I want to have a loop that checks if a key has been pressed during that time. The loop has a delay of Time.Sleep(25); So the loop starts, check if a key is down then continues with the code, the time delay hits then it repeats. The problem is I can't use ReadKey Read or ReadLine because these all stop the "flow" of the code causing it to physically halt, I want to code(loop) to continue if a key is pressed or not. And System.Windows.Forms; isnt an option as far as I know. Thanks guys.
Hi try using different thread in order to check if a key was pressed for example static void Main(string[] args) { object key = new object(); bool stopLoop = false; Thread t1 = new Thread(delegate() { while (Console.ReadLine() != "Q") ; lock (key) { stopLoop = true; } Console.WriteLine("Stop Command Issued"); }); t1.Start(); while (true) { lock(key) { if (stopLoop == true) break; } } }
-
Hi try using different thread in order to check if a key was pressed for example static void Main(string[] args) { object key = new object(); bool stopLoop = false; Thread t1 = new Thread(delegate() { while (Console.ReadLine() != "Q") ; lock (key) { stopLoop = true; } Console.WriteLine("Stop Command Issued"); }); t1.Start(); while (true) { lock(key) { if (stopLoop == true) break; } } }
-
Wow thanks! This is great, but is there anyway to use ConsoleKeyInfo .key ConsoleKey etc instead? Thanks in advance ^0^
-
Nevermind I figured it out on my own, all I had to do was change/add these two lines. ^O^. Thanks! ConsoleKeyInfo keyInput = new ConsoleKeyInfo(); while (Console.ReadKey().Key != ConsoleKey.Escape) ;
Ran into a slight problem. This performs actions within the while without stopping to check for the key but how do you add additional keys? So the loop goes constantly checking for an escape key press, and if it happens it breaks the loop. but what if I want it to perform actions based on other keys while its looping? If you got any info that would be great. =O) -- modified at 16:12 Monday 18th June, 2007 Don't worry again I think I cracked it.
-
Ran into a slight problem. This performs actions within the while without stopping to check for the key but how do you add additional keys? So the loop goes constantly checking for an escape key press, and if it happens it breaks the loop. but what if I want it to perform actions based on other keys while its looping? If you got any info that would be great. =O) -- modified at 16:12 Monday 18th June, 2007 Don't worry again I think I cracked it.
If anyone is interested in how this is done. I basically modified it so that it reads the key in the thread inside the loop over and over, and it does things according to whatever the key is. This is just an example. static void Main(string[] args) { object key = new object(); bool stopLoop = false; ConsoleKeyInfo keyInput = new ConsoleKeyInfo(); Thread t1 = new Thread(delegate() { while (keyInput.Key != ConsoleKey.Escape) // repeats the below code until... { keyInput = Console.ReadKey(); // Constantly checks for pressed key if (keyInput.Key == ConsoleKey.UpArrow) // does things depending on key { Console.WriteLine("Up Arrow"); } if (keyInput.Key == ConsoleKey.Escape) // the loop break { lock (key) { stopLoop = true; } } } }); t1.Start(); while (true) { Thread.Sleep(200); // Putting a delay in so it doesnt run at a billion mph Console.Write("01"); // Writing numbers just to visually check the speed lock (key) // This stops the loop if Escape is pressed as defined above. { if (stopLoop == true) break; } } } } }