C#
C#

help

Root Question Message

SantiJLM
SantiJLM1/3/2023
❔ Stop a loop when a key is pressed

I'm making an app with a while loop that is supposed to repeat over and over until the user hits a key. I alredy have a Key instance with the key I need.
SantiJLM
SantiJLM1/3/2023
I tried this:

bool loop = true; while (loop) { //Loop Stuff if (IsKeyDown(myKey)) { loop = false; } }
and this

while (loop) { //Loop Stuff if (IsKeyDown(myKey)) { break; } } But they didn't work. Also, saw this in StackOverflow: while(!Console.KeyAvailable) { //do work }

But apparently that won't work unless I use a Console Application.


A few days ago I tried the code in this StackOverflow post, but I see it too complicated, and don't understand anything. ¿Could someone explain? I hate the idea of just pasting a piece of code I don't even understand.
SantiJLM
SantiJLM1/3/2023
ACiDCA7
ACiDCA71/3/2023
bool loop = true;
while (loop)
{
//Loop Stuff
  if (Keyboard.IsKeyDown(Key.A))
  {
    loop = false;
  }
}

did a quick test and that code seems to work
ACiDCA7
ACiDCA71/3/2023
regarding your SO snippet
basicly what it does is that when the view opens the onloaded method gets called
in there is a loop that only fnishes when the cancelationtoken is set
the cancelationtoken get set on the onkeydownevent where before getting set it get checked if its the A key
FusedQyou
FusedQyou1/3/2023
Is this system.windows.input.keyboard.iskeydown?
FusedQyou
FusedQyou1/3/2023
Perhaps it works the same as Unity and it returns true the frame it was pressed?
FusedQyou
FusedQyou1/3/2023
I believe this has something to do with your cancellation token not properly cancelling and not really because of your key being pressed. async void has no proper context and I get the feeling it straight up loses it, causing your cancellation token to not properly cancel.
FusedQyou
FusedQyou1/3/2023
Not sure how to explain this, but I am pretty sure I had this issue before and I had the exact same scenario
ACiDCA7
ACiDCA71/3/2023
yes
ACiDCA7
ACiDCA71/3/2023
ive got no clue about unity
ACiDCA7
ACiDCA71/3/2023
i read his text as like he didnt even tried it yet since he doesnt understand it
FusedQyou
FusedQyou1/3/2023
You just gave me three answers while my whole message was aimed at @741000970879893557
SantiJLM
SantiJLM1/4/2023
I think I already tried this and didn't work
SantiJLM
SantiJLM1/4/2023
Lemme try it again
FusedQyou
FusedQyou1/4/2023
You should get used to debugging the problem a little better. Check using logging if Keyboard.IsKeyDown(Key.A) ever reaches true, and see why it does not respond to it.
SantiJLM
SantiJLM1/4/2023
I think I know why it does not work. The looping action includes a few System.Thread.Threading.Sleep. Probably that's why it doesn't get called.
SantiJLM
SantiJLM1/4/2023
I read in SO that I could make a timer, similar to Unity's update, and detect if my key is pressed every 100ms. That's when I realised how big my problem is.
SantiJLM
SantiJLM1/4/2023
The code I l'm making is going to be executed every time a button in the UI is pressed, so everything I do is inside of an event handler. Any idea of what I could do?
ContactFrequently Asked QuestionsJoin The DiscordBugs & Feature RequestsTerms & Privacy