C# Console Loop Proceed On KeyUp - c#

I need to make it to where my code will only exit the loop when the key that is pressed, is released. I am doing this is console and will need to stay in console.
Here is an example of the code.
ConsoleKeyInfo = ki;
while (true)
{
ki = Console.ReadKey();
if (ki.Key == ConsoleKey.A)
{
Console.Write("A");
}
}
As you can see above, when the A key is pressed, it will write A in the console. If you hold down the A key, it will continuously write A.
What I'm wanting is for the console to write A ONCE until the A key is released. Then, if you press A again, it will print again.
I've thought about using "KeyUp" but I'm not able to use it on a console application. But something that would accomplish the following...
ConsoleKeyInfo = ki;
while (true)
{
ki = Console.ReadKey();
if (ki.Key == ConsoleKey.A)
{
//I know the following isn't actually code, but it's explaining what I want to happen.
do (onKeyRelease)
{
Console.Write("A");
}
}
}
Basically, I'm wanting only one thing to happen when the loop when a key is pressed until the key is released. When the key is released, the loop will start again.
Please note that I am using this idea for a Text-Based RPG game. So waiting until another key is pressed (including adding another Console.ReadKey() to the end) would not be ideal.

1st Suggestion - Have you tried "break" the loop when the console has displayed the "A"?
2nd Suggestion -
ConsoleKeyInfo = ki;
var alreadyPressed = false
while (true)
{
ki = Console.ReadKey();
if (ki.Key == ConsoleKey.A && !alreadyPressed)
{
Console.Write("A");
alreadyPressed = true;
}}`

Store last pressed key and print character only when it is different from the last pressed key.

Related

How to use Console.ReadKey() while avoiding the same key being registered constantly after, C#

I would like to make a program which will let the user decide what program to run, by pressing certain keys. I have now come so far that some of these keys work perfectly. However I have now come to a problem that I have a difficult time to solve. Now when I press a key it activates the same thing that was activated before even though I press a different key. I believe that the issue lies in the WaitForKey()-method, but I am not sure where in there. Can you help me to locate the issue and bring in some solutions to this?
note: hasBeenPressed is a Boolean value which tells if the key has already been pressed or not and if so it should avoid activating some other functionality automatically.
public static void WaitForKey(ConsoleKey key) {
ConsoleKeyInfo keyInfo = Console.ReadKey(true);
if (keyInfo.Key == key && hasBeenPressed == false) {
hasBeenPressed = true;
return;
}
else if (keyInfo.Key == key) {
hasBeenPressed = false;
return;
}
}
I use the WaitForKey() in this context:
for (int i = 0; i < mySignalChain.Count - 1; i++) {
if (keyPress.Key == ConsoleKey.I) {
pedalsActivated(mySignalChain)
WaitForKey(ConsoleKey.I);
}
I hope this was clear otherwise I will try to elaborate on this.
Thanks in advance!
Based on the description of what you would like to achieve,
I would like to make a program which will let the user decide what program to run, by pressing certain keys.
I wrote the following Console Application that will execute a different function depending on the key you press (I, J, or K), and will keep asking for keys until the user presses a different key, in which case the program will finish.
var exit = false;
ConsoleKeyInfo keyInfo;
do
{
Console.WriteLine("Please enter a new Key");
keyInfo = Console.ReadKey();
Console.WriteLine();
switch (keyInfo.Key)
{
case ConsoleKey.I:
FunctionFor_I();
break;
case ConsoleKey.J:
FunctionFor_J();
break;
case ConsoleKey.K:
FunctionFor_K();
break;
default:
ExitProgram();
break;
}
}
while (exit == false);
void FunctionFor_I()
{
Console.WriteLine("I has been pressed");
}
void FunctionFor_J()
{
Console.WriteLine("J has been pressed");
}
void FunctionFor_K()
{
Console.WriteLine("K has been pressed");
}
void ExitProgram()
{
exit = true;
}
I tried to replicate your code first and see how it works, but I couldn't understand its purpose so I focused on your textual description of the problem. If this is not what you intended, please post the full version of your code and I'll try to reproduce it.
I have now fixed my issue (somewhat) by doing the following:
ConsoleKeyInfo keyPress = Console.ReadKey(true);
while (keyPress.Key != ConsoleKey.Q) {
switch (keyPress.Key) {
case ConsoleKey.I:
pedalsActivated(mySignalChain);
Console.WriteLine("");
WaitForKey(ConsoleKey.I);
keyPress = Console.ReadKey(true);
break;
.
.
.
}
By using my function WaitForKey() defined above I can now activate my functions while at the same time avoiding a certain key being spammed. But then I also reset the keyPress (by: keyPress = Console.Readkey(true);) so that other functions can be called with other keys - though I have to press every key twice now to do so. This works better than having to press the keys far more than twice - however it would be optimal if I would only need to press each key once. And if anyone has an idea how to do so it would be much appreciated.

C# - System.Console respond to "ConsoleKey.Pause" (PauseBreak key)

I have pause functionality in my application that I would like to interact with via keypresses in the System.Console window, and so the aptly-named ConsoleKey.Pause seems correct to use since the documentation says it corresponds to "the PAUSE key."
But whenever I press the key explicitly labeled Pause on my keyboard, Console.ReadKey never does anything:
while (_isOpen)
if (Console.ReadKey(false).Key == ConsoleKey.Pause)
_engine.TogglePause();
So what does ConsoleKey.Pause represent, and how might I trigger it? Or should I just invent my own shortcuts?
try below code:
while (_isOpen)
{
//Do you works need
if (Console.KeyAvailable)
{
var consoleKey = Console.ReadKey(true); //if true then keeps the key from
//the console
if (consoleKey.Key == ConsoleKey.Pause)
{
_engine.TogglePause();
}
}
}

Reset clock when enter key is pressed

I have created a 24 hour clock and I want to be able to reset it to 00:00:00 when the enter key is pressed. The clock itself functions fine but I am unable to include the reset functionality properly
static void Main(string[] args)
{
Clock _clock = new Clock();
ConsoleKeyInfo _key = Console.ReadKey();
while (true)
{
Console.WriteLine(_clock.Time);
_clock.Tick();
System.Threading.Thread.Sleep(1000);
if (_key.KeyChar == (char)13)
{
_clock.Reset();
}
}
The problem is, you only read the current keypress once- at the very beginning of your program. Console.Readkey() will wait for you to press a key, then return the key you pressed (which then gets stored in _key). Because you never read another keypress inside the loop, _key will never change.
Of course, you can't simply call Console.ReadKey() inside the loop or the program will pause and wait for you to press a key during every iteration, so you'll want to follow the advice Aybe linked to in his comment.

Reading Key Press in Console App

I'm writing a small application which will wait for a key press, then perform an action based on what key is pressed. For example, if no key is pressed, it will just continue waiting, if key 1 is pressed, it will perform action 1, or if key 2 is pressed it will perform action 2. I have found several helpful posts so far, this one being an example. From this, I have the following code so far.
do
{
while (!Console.KeyAvailable)
{
if (Console.ReadKey(true).Key == ConsoleKey.NumPad1)
{
Console.WriteLine(ConsoleKey.NumPad1.ToString());
}
if (Console.ReadKey(true).Key == ConsoleKey.NumPad2)
{
Console.WriteLine(ConsoleKey.NumPad1.ToString());
}
}
} while (Console.ReadKey(true).Key != ConsoleKey.Escape);
There's a couple of issue with this, which you may have guessed already.
1)
Using the ReadKey to detect which key is pressed results in a temporary pause, meaning the key will have to be pressed. I really need to find a way to avoid this. Possibly using KeyAvailable, but I'm not sure how you can use this to detect which key has been pressed - any ideas?
2)
For some reason, the Escape key does not escape out of the application. It does if I remove the if statements, however running the code as is above does not let me exit the application using the designated key - any ideas?
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);

How do you exit an application with a press of a specific key?

I'm doing a small c# application.
(Press 0 to end the program)
All I know is:
Console.ReadKey(); but that only works when you press ANY key.
But how do you exit an application with just 0?
you don't need to run the while loop to findout the key.
you have to compare with the what ever users enters the key.
ConsoleKeyInfo info= Console.ReadKey();
if (info.KeyChar == 48)
Environment.Exit(0);
else
{ // do your things
}
First of all your program will run in a while loop. Read the entered word and check in if condition that whenever it is 0 or not. If it is 0 then let the program finish by breaking the loop otherwise use continue. Let me know if you have any confusion.
You can use while loop :
ConsoleKeyInfo cki = new ConsoleKeyInfo();
while (cki.Key != ConsoleKey.D0 && cki.Key != ConsoleKey.NumPad0)
{
cki = Console.ReadKey(true);
// your code here
}

Categories