Home Gadgetman Forum Book Reviews Links Contact me X-UFO
Gadgets Processors Programming School Old news
The Event loop is in many respects the most important part of an App.
This part of the App not only has to handle user initiated events, but it must also cope with events from the system.

we can divide the events into 2 groups:

Here's how to:
(This code requires that you have the line INCLUDE "Const.oph" in the beginning of your source code)
PROC eventloop:
LOCAL eventdata&(16) REM to store the event information....
      GETEVENT32 eventdata&()   REM Waits for something to happen, and then store it in the array.
      IF (eventdata&(1) AND KEvNotKeyMask&) = 0  REM If this is true it was a keypress.
      ELSEIF eventdata&(1) = KEvCommand&     REM System command(C/O/X)?
      ELSEIF eventdata&(1) = KEvPtr&         REM Pointer event?
      ELSIF eventdata&(1) = KKeySidebarMenu% REM not necessary, but REALLY should be used. It detects if the
         handlemenu:                         REM user uses the pen to activate the menu.
            REM forget about the ones you don't use
   UNTIL a_certain_warm_place_freeze_over

Simple, eh...

To recap: Use a 16 element LONG INTEGER(&) array to store the information from GETEVENT32
Use DO UNTIL. Do not use testevent unless you want to empty your batteries.(it's useful in games and other 'realtime' apps, and of course in processor intensive parts of your app to test for a cancel... :-)
Use IF ELSEIF ...ENDIF to find out what kind of event occured, and to run the correct PROC.

Be careful to handle ALL relevant events.(The Focus events 'lostfocus', 'gainedfocus' isn't that important to a word processor, but should be considered in f.example Games, so that the game is paused when in background.

The KKeySidebarMenu% event REALLY should be tested for. It signals that the user pointed the stylus at the menu symbol at the upper left corner of the screen. It should be handled the same way you would handle the [Menu] Button. Not doing this can ruin the look and feel of the program, and cause the user to dump it! (I personally don't register pen avare Apps that don't use this )

BTW: Keep this PROC as short as possible...
Doing more testing than absolutely necessary to select the correct PROC to call will just result in an app that is slower to respond to ALL other events...

<<5. Setting up the Data>>
<<How to build an application>>
<<7. Handling the [Menu] key>>