06-26-2024, 03:45 PM
I think Elzaimer is asking why there is no specific command in Small Basic to wait until a key is pressed in the GraphicsWindow. Why he has to implement a small wait loop to handle this.
Long answer -
At a low level the OS is running message loops continually checking for events and passing them to other applications that have their own message loops; they are everywhere.
Google about message loops, for example https://en.wikipedia.org/wiki/Message_lo...ft_Windows.
Small Basic gets events by requesting that it is informed by the OS when for example a key is pressed while the GraphicsWindow has focus - so when a key press is detected by th OS it is dispatched (passed around) from OS to application to window etc through layers of message loops until it finally calls your SB event subroutine.
How we handle that event (or what we do while waiting for it) is then up to us in our SB program. Nonki's program showed one way, and I showed another. In both cases the program keeps looking for the event and while waiting does nothing except going round its loop. Despite this seeming odd, this is totally standard practice.
If you are concerned about wasting cpu while the loop is running you could put a small delay in the loop, say Program.Delay(10), but I wouldn't since you are interested in reaction times it would mess timings up a bit and is unnecessary. If the loop does nothing it consumes no resources, allocates no memory or other things that would adversely affect other programs and is probably mostly optimised away by the compiler and will be insignificant compared to everything else going on. Remember every other process is running their own message loops (getting messages about all sorts of things), including passing instructions to the cpu and gpu at extremely high rates. The OS is quite clever at prioritising/optimising itself.
If we added a SB method to wait for next key press, it would be implemented as a message loop waiting for the event.
Short answer -
Thats how it is with programming - somethimes there isn't a ready made command.
There are other issues as well. Consider why the code below doesn't work as maybe expected.
It will be instructive to work on this a bit and also Nonki's code.
The fix I gave was probably not the best way to write your program, but the simplest 'Fix' - and more infromation was given in the articles I posted links to.
Code:
frappe = ""
While (frappe = "")
EndWhile
Long answer -
At a low level the OS is running message loops continually checking for events and passing them to other applications that have their own message loops; they are everywhere.
Google about message loops, for example https://en.wikipedia.org/wiki/Message_lo...ft_Windows.
Small Basic gets events by requesting that it is informed by the OS when for example a key is pressed while the GraphicsWindow has focus - so when a key press is detected by th OS it is dispatched (passed around) from OS to application to window etc through layers of message loops until it finally calls your SB event subroutine.
How we handle that event (or what we do while waiting for it) is then up to us in our SB program. Nonki's program showed one way, and I showed another. In both cases the program keeps looking for the event and while waiting does nothing except going round its loop. Despite this seeming odd, this is totally standard practice.
If you are concerned about wasting cpu while the loop is running you could put a small delay in the loop, say Program.Delay(10), but I wouldn't since you are interested in reaction times it would mess timings up a bit and is unnecessary. If the loop does nothing it consumes no resources, allocates no memory or other things that would adversely affect other programs and is probably mostly optimised away by the compiler and will be insignificant compared to everything else going on. Remember every other process is running their own message loops (getting messages about all sorts of things), including passing instructions to the cpu and gpu at extremely high rates. The OS is quite clever at prioritising/optimising itself.
If we added a SB method to wait for next key press, it would be implemented as a message loop waiting for the event.
Short answer -
Thats how it is with programming - somethimes there isn't a ready made command.
There are other issues as well. Consider why the code below doesn't work as maybe expected.
Code:
GraphicsWindow.Show()
GraphicsWindow.KeyDown = OnKeyDown
For i = 1 To 100
key = ""
While (key = "")
EndWhile
EndFor
Sub OnKeyDown
key = GraphicsWindow.LastKey
GraphicsWindow.Clear()
GraphicsWindow.DrawText(10,10,"Test "+i+" key = "+key)
EndSub
It will be instructive to work on this a bit and also Nonki's code.
The fix I gave was probably not the best way to write your program, but the simplest 'Fix' - and more infromation was given in the articles I posted links to.