We would like to build a community for Small Basic programmers of any age who like to code. Everyone from total beginner to guru is welcome. Click here to register and share your programming journey!


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
What command should I use to make my program pause until the user presses a key?
#1
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 ! Smile
[-] The following 1 user Likes Elzaimer's post:
  • Nonki Takahashi
Reply
#2
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 Smile .

Your program looks fun and I look forward to seeing it fully working.
[-] The following 1 user Likes litdev's post:
  • Elzaimer
Reply
#3
Hi Elzaimer, I'd like to show my old program 'Key Logger' (http://smallbasic.com/progarm/?PFSH388.000). I hope this will help you.
[-] The following 2 users Like Nonki Takahashi's post:
  • Elzaimer, litdev
Reply
#4
(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.

Shy
[-] The following 1 user Likes AbsoluteBeginner's post:
  • Elzaimer
Reply
#5
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/archiv...ent-basics
https://learn.microsoft.com/en-us/archiv...c-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
  frappe = ""
  While (frappe = "")
  EndWhile
[-] The following 3 users Like litdev's post:
  • AbsoluteBeginner, Elzaimer, z-s
Reply
#6
Thank you LitDev, Nonki and AB !

I think I understand but I have to try to confirm it  Smile
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.
[-] The following 1 user Likes Elzaimer's post:
  • AbsoluteBeginner
Reply
#7
Elzasmer, I sent you a private message.  Shy
[-] The following 1 user Likes AbsoluteBeginner's post:
  • Elzaimer
Reply
#8
Yeeeeehaaaaahhhh... it works !!!   Big Grin

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 ?
[-] The following 2 users Like Elzaimer's post:
  • AbsoluteBeginner, litdev
Reply
#9
The rounding is a bit of a SB quirk

Code:
a = 1
TextWindow.WriteLine(Math.Round(a*100)*0.01)
a = Math.Pi
TextWindow.WriteLine(Math.Round(a*100)*0.01)
[-] The following 1 user Likes litdev's post:
  • Elzaimer
Reply
#10
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.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)