C#C
C#3y ago
waler

C# help with detecting key press

Hello, I'm a beginner to c# and I'm currently doing this practice exercise.
The exercise requires me to code out a character falling from the top of the compiler window to the bottom. This task will run until the user press the Escape key to then quit the task.
So I know how to make the character fall from the top of the screen to the bottom, but I'm not sure how I can make it do that asynchronously while waiting for a key press to quit.

I have searched online for a way to detect a keypress while executing a loop and found this KeyAvailable property that I can use. However, as to my understand, the KeyAvailable property only returns true when a key is pressed (that is if I press a key during the inner while loop is executed, it wouldn't actually stop the loop and exit it, to do that I must actually hold down the Esc key until the inner while loop finishes (or press the Esc exactly at the very end of the loop) to check the condition !KeyAvailable)

Could anyone recommend me an alternative to this?
Screenshot_2023-09-03_125701.png
Was this page helpful?