Microsoft KB Archive/64430

= Abrupt Branch to ON Event GOSUB Handler from Separate Handler =

Article ID: 64430

Article Last Modified on 8/16/2005

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 7.1

-



This article was previously published under Q64430



SUMMARY
When program control is within an ON GOSUB handler, it is still possible to trap other events (where can be COM, KEY, PEN, PLAY, STRIG, TIMER, etc.). This is normal behavior for ON GOSUB trapping, but may be undesirable for those who want to disable all event trapping within an ON handler. This article gives a code example demonstrating normal flow of control when a second trappable key is pressed within a given ON KEY GOSUB handler. The comments in this program show a method of temporarily disabling KEY and other event trapping within an ON KEY GOSUB handler.



MORE INFORMATION
Within an ON GOSUB handler, event trapping is suspended within the handler for only the trapped event. Any other active event traps (set with other ON GOSUB statements) triggered within an event handler will cause control to immediately branch to the other ON handler during execution of the initial handler. Upon terminating the second handler, control resumes where it left off in the original handler. This abrupt transfer of control may be problematic for those who want all actions surrounding a particular event, a key press for example, to be processed in full before subsequent key presses or events are handled.

The code example below demonstrates a case of control branching from one ON KEY handler to another when a second key is pressed within the first handler. The example contains the code (remarked out) necessary to temporarily disable all ON KEY and other event trapping within the initial ON KEY handler. If you unremark (uncomment) the code shown, the program will complete each handler without interruption.

Code Example
'DECLARE SUB EventsStop  ' These are the DECLARE statements for the 'DECLARE SUB EventsOn    ' SUBs that disable and enable key and ' event trapping. (DECLARE statements are                           ' not needed or supported for QuickBasic                            ' 3.00 or earlier versions.) CLS

ON KEY(1) GOSUB F1KeyHandler  ' Trapping for F1 function key. ON KEY(2) GOSUB F2KeyHandler  ' Trapping for F2 function key.

KEY(1) ON KEY(2) ON

PRINT &quot;Please, press the F1 key&quot; SLEEP PRINT &quot;Exiting program&quot; END

' To temporarily disable all key and event trapping within an ' event handler, the CALL to SUB EventsStop must be the first ' statement and the CALL to SUB EventsOn must be the last statement ' of the handler before the RETURN.

F1KeyHandler: 'CALL EventsStop      ' Include this statement to temporarily ' disable all or selected ON KEY and ' event statements

PRINT &quot;In F1 key handler&quot; PRINT &quot;Press F2 to jump to F2 key handler&quot; SLEEP PRINT &quot;Exiting F1 key handler&quot;

'CALL EventsOn        ' Include this statement so that any ' keys pressed or events occurring ' during the execution of this handler ' will be processed at this point. RETURN

F2KeyHandler: 'CALL EventsStop      ' Include this statement to temporarily ' disable all or selected key and event ' trapping within this handler. PRINT &quot;In F2 key handler&quot; PRINT &quot;Exiting F2 key handler&quot;

'CALL EventsOn        ' Include this statement so that ' any trappable keys or events triggered ' during the execution of this handler ' will be processed at this point. RETURN

SUB EventsOn              'All keys and events to enable go in here KEY(1) ON       KEY(2) ON

'TIMER ON         'Examples of events that may be enabled 'COM(1) ON END SUB

SUB EventsStop            'All keys and events to temporarily disable KEY(1) STOP       'go in here KEY(2) STOP

'TIMER STOP       'Examples of events that may be temporarily 'COM(1) STOP      'disabled END SUB

