trapping a key before it gets to the Console - c#

THE SETTING:
A Console program, in .net
I am using readkey in a loop, which rewrites to the same line on the screen (Console.setCursorPosition)
however, if I type Enter, the Console pushes the text upwards, which is the usual command line behaviour.
THE QUESTION:
Is it possible to trap a key press (that Enter), so that my program will get the key, but the Console does not?
The environment is Linux, with Mono (and the program is supposed to be cross platform). so low level windows driver intercepting is not an option.
PS: I know the method of clearing and redrawing everything. I would like to know if my question is possible.
Thank you for any information

after modifying code found here https://stackoverflow.com/a/8898251/1951298 I came to something that may help you, you see that when user pushes Enter the cursor doesn't increment, and nothing is written into console
ConsoleKeyInfo keyinfo;
int left, top=0;
do
{
left = Console.CursorLeft;
top = Console.CursorTop;
keyinfo = Console.ReadKey(true);
if(keyinfo.Key.ToString().Equals("Enter"))
Console.SetCursorPosition(left,top-1);
else
Console.WriteLine(keyinfo.Key + " was pressed");
}
while (keyinfo.Key != ConsoleKey.X);

Console.ReadKey(true) should read the key but not show it.

You should intercept the key press. MSDN Documentation
var PressedKey = Console.ReadKey(true)

Related

C# console application randomly waits for input

i made an app and sometimes it just pauses until I press a key.
i do not use any code which requiereres any input or interaction (such as readline or readkey).
this randomly happens like 1 in 20 times after Thread.Sleep and after you press any key it continues to work perfectly.
if (Convert.ToInt64(timetotask[0]) > 0)
{
Thread.Sleep(Convert.ToInt32(timetotask[0]));
}
else
{
mylog.log("Task was in the past, executing it now");
}
int currentbid = placebid(task.itemid, bid, driver[Convert.ToInt32(task.account)]);
the first line of placebid is console.writeline("mystring") so i do not see anything what could cause this behaviour.
Does it have something to do with debug mode?
thanks for help
This could be obvious but if pressing a key in the console makes the application resume, it is most likely you managed to pause the app via the console by pressing a key.
When it is sleeping do you ever press a key to check if it has paused? Then eventually the sleep ends as it matches the rule and then when you next press a key it unpauses the program...
Michael

better end on keypress

I am writing a console app and found this method to end a loop on a keypress:
while (!Console.KeyAvailable){//do stuff}
It works, but it echos the key that was pressed back to the prompt. Is there a better method?
edit:
To clarify more, the loop runs and if hit the letter j the loop ends and the program exits. However, I get the following output at the prompt:
C:\>j
If you want to exit from a loop after a particular key is pressed, then instead of Console.KeyAvailable you can use the Console.ReadKey() function and check the return type of this function as an exit condition.
Here is the implementation.
while (true) {
var keyPressed = Console.ReadKey(true);
if (keyPressed.KeyChar == 'j') break;
//do something
Console.WriteLine("Key pressed: " + keyPressed.KeyChar);
}
For more details: C# Console - hide the input from console window while typing

C# Console.In.Peek(); returns endless results after initial key press

I'm trying to create a thread which I check for key presses in a console application. The idea being if no keys are pressed for 10 minutes the user is logged out.
I spawn this little thread where I try to use Console.In.Peek(); to get a key press without reading the key so the main thread can actually ReadKey / ReadLine issue free. However, I am finding that upon pressing the very first key Console.In.Peek(); stops blocking and that loop goes over and over without obstruction.
Any ideas why Console.In.Peek(); stops blocking after the first key press and any ideas how to fix it? Thanks so much!
public static void AuthTimer()
{
while (true)
{
Console.In.Peek();
Thread.Sleep(1000);
//Console.ReadKey();
Console.WriteLine("auth timer");
if (Authentication.Authenticated == true)
{
Authentication.AuthTime = DateTime.Now;
}
}
}
EDIT EDIT EDIT
Thanks to the comments here by the fine folks on Stackoverflow I gave up on Console.In.Peek() and switched to
if (Console.KeyAvailable == true)
It doesn't take the key out of the stream and seems to block until a key is pressed so it effectively solves my issue! Thanks everyone for the comments.

C# Console - Run Event on Key Press

I just wanted to ask if it was possible to make an event run if the player presses a specific button. This should be tested at every point in the console application but not if there is a Console.ReadLine() event used at the moment.
Is there a way to do this?
You can use Console.ReadKey() method like this
ConsoleKeyInfo keyInfo = Console.ReadKey();
if(keyInfo.Key == ConsoleKey.A)
{
}
For more info see https://msdn.microsoft.com/en-us/library/system.console.readkey(v=vs.110).aspx

Disabling input in C# Console until certain task is completed

I'm working on a little part of my program, handling the input, basically I have this little code:
bool Done = false;
while (!Done)
{
ConsoleKeyInfo key = Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
{
//Action
}
}
The main problem with this is that the code will handle the ReadKey even between actions.
So if you have a menu where you can press keys and then it would say "you pressed: x" if you press any buttons while it shows you this message, the ReadKey already gets that new key.
So I want to block any further input until the user sees the menu again.
Not so sure this make sense, personally I like it when keystrokes don't disappear and I can type ahead. But you can flush the keyboard buffer like this:
while (!Done)
{
while (Console.KeyAvailable) Console.ReadKey(true);
ConsoleKeyInfo key = Console.ReadKey(true);
// etc..
}
You can not block input,
Even if you do not process it, it goes to the keyboard buffer.
You can simply stop getting them out of the buffer though.

Categories