How I can Check keyboard buffer?
-
Hi! Please help me! I hava a problem. How I can check Keyboard buffer is empty or not? For instance do { // do something } while (CharFromKeyBoard != "q"); System.Read and SystemReadLine waiting user input, but I need do some operation until user press some key, for instance char "q". Sorry my English.
-
Hi! Please help me! I hava a problem. How I can check Keyboard buffer is empty or not? For instance do { // do something } while (CharFromKeyBoard != "q"); System.Read and SystemReadLine waiting user input, but I need do some operation until user press some key, for instance char "q". Sorry my English.
Try something like this (especially
StreamReader.Peek()
):StreamReader reader = new StreamReader(Console.In);
try
{
reader = new StreamReader(Console.In); // Or whatever Stream.
do
{
// Do something.
} while (reader.Peek() != 113); // Might want to check 81, too.
}
finally
{
if (reader != null)
reader.Close();
}
Reminiscent of my younger years... 10 LOAD "SCISSORS" 20 RUN
-
Try something like this (especially
StreamReader.Peek()
):StreamReader reader = new StreamReader(Console.In);
try
{
reader = new StreamReader(Console.In); // Or whatever Stream.
do
{
// Do something.
} while (reader.Peek() != 113); // Might want to check 81, too.
}
finally
{
if (reader != null)
reader.Close();
}
Reminiscent of my younger years... 10 LOAD "SCISSORS" 20 RUN
This sample doesn't work. The best overloaded method match for 'System.IO.StreamReader.StreamReader(System.IO.Stream)' has some invalid arguments Argument '1': cannot convert from 'System.IO.TextReader' to 'System.IO.Stream' :zzz:
-
This sample doesn't work. The best overloaded method match for 'System.IO.StreamReader.StreamReader(System.IO.Stream)' has some invalid arguments Argument '1': cannot convert from 'System.IO.TextReader' to 'System.IO.Stream' :zzz:
That exception message is self-explanitory - you should be able to figure that one out. I just threw this sample together quick, but you have to learn to research problems yourself or you're really in trouble. If
Console.In
is aTextReader
, then take a look at its methods orStreamReader
's constructors. The former yields thatTextReader.Peek
does exist so you don't even have to create a new class instance. Just useConsole.In.Peek
to look at the next character in the buffer. Researching the docs is at least half of what development is.
Reminiscent of my younger years... 10 LOAD "SCISSORS" 20 RUN
-
Hi! Please help me! I hava a problem. How I can check Keyboard buffer is empty or not? For instance do { // do something } while (CharFromKeyBoard != "q"); System.Read and SystemReadLine waiting user input, but I need do some operation until user press some key, for instance char "q". Sorry my English.
I know this is some years late but you might still be interested. do { } while(!Console.KeyAvailable) or do { } while(!Console.KeyAvailable && Console.ReadKey().KeyChar == 'q')
Thats the sound of Inevitability ....