C
C#2y ago
santijlm

❔ 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.
10 Replies
santijlm
santijlmOP2y ago
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
santijlmOP2y ago
ACiDCA7
ACiDCA72y ago
bool loop = true;
while (loop)
{
//Loop Stuff
if (Keyboard.IsKeyDown(Key.A))
{
loop = false;
}
}
bool loop = true;
while (loop)
{
//Loop Stuff
if (Keyboard.IsKeyDown(Key.A))
{
loop = false;
}
}
did a quick test and that code seems to work 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
FusedQyou2y ago
Is this system.windows.input.keyboard.iskeydown? Perhaps it works the same as Unity and it returns true the frame it was pressed? 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. Not sure how to explain this, but I am pretty sure I had this issue before and I had the exact same scenario
ACiDCA7
ACiDCA72y ago
yes ive got no clue about unity i read his text as like he didnt even tried it yet since he doesnt understand it
FusedQyou
FusedQyou2y ago
You just gave me three answers while my whole message was aimed at @SantiJLM
santijlm
santijlmOP2y ago
I think I already tried this and didn't work Lemme try it again
FusedQyou
FusedQyou2y ago
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
santijlmOP2y ago
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. 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. 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?
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server