These tutorials use the 16-bit version of RMR Software's excellent "RMREvent" example program, disecting it bit by bit to show you how and why it works. Make sure you've got the latest version, v1.00.
We commented last time that most of RMREvent's working life is spent going round and round the loop:
DO
Run:
UNTIL 0
Obviously, the condition "0" (i.e. zero) is never going to equal one, true, or anything else worth evaluating, so the DO loop just carries on forever. Effectively RMREvent is saying "Run forever, handling each event as it happens". Typical events might be a user pressing a key or the system saying "Shut down now".
Let's look further at the Run: procedure:
Notice that this too is basically just one big loop. There are many ways to arrange 'event loops' and RMR's standard technique is to have the main one nested 'inside' a higher level loop. This is done so that when things go wrong (i.e. the ONERR statement is needed) things can be handled (through the GoError: procedure) and control returned to Run:'s loop via the top level one.
Each time through the loop, GETEVENT Event%() is called. Event%(10) is an array of integers defined right at the start of the main code which is destined to receive detailed information on all events detected within the running program. GETEVENT works a bit like the simple GET command in that it hangs around until there's something there (i.e. it doesn't just carry on returning zero or similar). It's also very battery-efficient, waiting with almost no battery drain at all. Contrast this with the more clumsy technique, often used by beginners, of looping round very fast, testing KEY to see which keys have been pressed.
The code within the loop is really just a list of tests of possible values that GETEVENT can return. The first few are hopefully obvious (look ahead to the appropriate procedures to see exactly what happens when each is detected):
It's the section at the end of the loop which is most interesting. Event%(1) and Event%(2) get set by GETEVENT to the keycode and the 'modifier' used. Both these two need a note of explanation:
Once a value for Mod% has been obtained (i.e. we know which of Control, Shift, Psion were held down during the last keypress), procedure Modify: simply sets up integer 'flags' (Control%, Shift% etc) for our convenience when considering what to do with the keypress.
Finally, the keycode itself is investigated in detail, in procedure Key:
As you can see, this section of code is nothing more than a very long list of IF...ELSEIF...ENDIF conditions. When a match is found, the program knows what to do etc. This is where you'd put the calls to your custom procedures that make up the guts of your specific program, e.g. calculate:, display:, printout: etc. or whatever your program is supposed to do.
See you next week, when we'll be looking further at hot-keys and their coexistence with a well-designed menu.
Go to next lesson | Programming index