What command should I use to make my program pause until the user presses a key? - Printable Version +- Small Basic Forum (https://litdev.uk/mybb) +-- Forum: Small Basic (https://litdev.uk/mybb/forumdisplay.php?fid=1) +--- Forum: Standard Small Basic (https://litdev.uk/mybb/forumdisplay.php?fid=2) +--- Thread: What command should I use to make my program pause until the user presses a key? (/showthread.php?tid=93) |
What command should I use to make my program pause until the user presses a key? - Elzaimer - 06-24-2024 Hello everyone, I'm new on this Forum and also new to programming... I'm trying to code a test to assess the ability to concentrate (https://smallbasic.com/program/?RKCP997.000) but I'm encountering a problem. This is how it should work: A letter appears on the screen for 0.5 seconds then it disappears. You have to memorize it. Then 4 random letters appear and you must press as quickly as possible on the "Left" key if the original letter is absent and on the "Right" key if it is present. So far my code works. But this test should be repeated for 2 minutes, and at the end the number of errors and the average reaction time should be displayed. But when I make a loop (lines 13 and 38 in my program), the program loops without waiting for the user to press LEFT or RIGHT. What command should I use to make it pause until the user presses a key? Thanks for your help ! RE: What command should I use to make my program pause until the user presses a key? - litdev - 06-24-2024 Welcome Elzaimer! This forum is fairly new, but we have have some great people here. I would like to give some of them a chance to help you with your question before me, so that we can develop the community to all help each other . Your program looks fun and I look forward to seeing it fully working. RE: What command should I use to make my program pause until the user presses a key? - Nonki Takahashi - 06-24-2024 Hi Elzaimer, I'd like to show my old program 'Key Logger' (http://smallbasic.com/progarm/?PFSH388.000). I hope this will help you. RE: What command should I use to make my program pause until the user presses a key? - AbsoluteBeginner - 06-24-2024 (translated by Google translator) Hello Elzaimer. I don't speak English well. Therefore, it is difficult for me to explain my thoughts to you. But I'll try. Although I am also a newbie, I see that you declared an event handler subroutine " GraphicsWindow.KeyDown = test " inside the loop. And now, the subroutine is declared every time the loop is executed. And the handler subroutine must be declared once at the beginning of the code of the entire program. Also, there doesn't seem to be any code in your "test" routine that tells the program what to do if no key is pressed. Therefore, the program does not stop and does not wait for a key to be pressed. And I also know that the event handler routine should contain as little code as possible. You can see this in some example. RE: What command should I use to make my program pause until the user presses a key? - litdev - 06-24-2024 Hi again, When an event occurs we need to handle it (do something) - in your code you do it directly in the event subroutine 'test'. This is OK, but sometimes this can cause problems (mainly because the event happens on a separate thread) and then it may be better the create a repeating loop (called game or event loop) that keeps looping and checks on a variable set in the event subroutine (we call this variable a flag). This is the essense of the nice sample Nonki shared and the comments by AB about event subroutine being short. AB is also correct that the event subroutine only needs to be set once. For more information on this see the links below. https://learn.microsoft.com/en-us/archive/technet-wiki/15060.small-basic-event-basics https://learn.microsoft.com/en-us/archive/technet-wiki/20865.small-basic-dynamic-graphics https://learn.microsoft.com/en-us/archive/technet-wiki/22264.small-basic-threading Without refactoring your code a lot, we need to wait after T0 is set until a key is pressed. The code below is one way we could do this. Code: T0 = Clock.ElapsedMilliseconds RE: What command should I use to make my program pause until the user presses a key? - Elzaimer - 06-24-2024 Thank you LitDev, Nonki and AB ! I think I understand but I have to try to confirm it I'll let you know if I succed in implementing your advices. EVENTs are a bit strange to me. Indeed, I am 54 years old and the last time I programmed I was 15 or 16 years old. It was in GFA BASIC on ATARI 520ST and AMIGA 500, almost 40 years ago... EVENTs didn't exist and I have to get used to them. Excuse my english. I am french and my english school lessons are... old. RE: What command should I use to make my program pause until the user presses a key? - AbsoluteBeginner - 06-24-2024 Elzasmer, I sent you a private message. RE: What command should I use to make my program pause until the user presses a key? - Elzaimer - 06-24-2024 Yeeeeehaaaaahhhh... it works !!! my first program since 40 years ! link : SQRT610.000 I tried to use all your tricks I will now make some cosmetic changes Thank you very much guys ! p.s. how to limit the average reaction time display to 2 decimals ? RE: What command should I use to make my program pause until the user presses a key? - litdev - 06-24-2024 The rounding is a bit of a SB quirk Code: a = 1 RE: What command should I use to make my program pause until the user presses a key? - Elzaimer - 06-26-2024 Finally, I find it so weird that there is no function for : waiting the user to hit a key with pausing the program. a sort of "wait for keyboard input". This would leave the CPU idle. |