Q(uick)BASIC Statement: EVENT ON
Quick View
EVENT ON
A statement that enables trapping of events
Worth knowing
Useful and cross-version information about the programming environments of QBasic and QuickBasic.
Syntax
- EVENT ON
- EVENT OFF
Description/Parameter(s)
Usage Notes
- The EVENT statements affect compiled code generation. When EVENT OFF is in effect, the compiler will not generate any event-checking code.
- EVENT OFF is equivalent to compiling a program from the BC command line without using /V or /W. EVENT OFF can be used to create small and fast code that still supports event trapping.
- If your program contains event-handling statements and you are compiling from the BC command line, use the BC /W or /V option. (The /W option checks for events at every label or line number; the /V option checks at every statement.) If you do not use these options and your program contains event traps, BASIC generates the error message, "ON event without /V or /W on command line."
- EVENT OFF and EVENT ON can be used to bracket sections of code where events do not need to be detected and trapped, and fast performance is required. Events still could be detected and tracked if a routine using EVENT ON were called from a section of EVENT OFF code.
- If an event occurs while EVENT OFF is in effect, this event is still remembered and then is handled as soon as an EVENT ON block is entered.
Example
This example uses the EVENT and STRIG statements to enable joystick event trapping for two buttons on each of two joysticks. The ON STRIG statement passes control to an event-handling routine when a button is pressed. The STRIG function is used to determine which button was pressed, and the STICK function returns the position of both joysticks.
CLS
'Turn on event processing.
EVENT ON
'Enable the upper and lower buttons on both joysticks.
STRIG(0) ON 'Lower button, joystick A
STRIG(2) ON 'Lower button, joystick B
STRIG(4) ON 'Upper button, joystick A
STRIG(6) ON 'Upper button, joystick B
'Set up joystick event processing for each button.
ON STRIG(0) GOSUB JoyButtonHandler
ON STRIG(2) GOSUB JoyButtonHandler
ON STRIG(4) GOSUB JoyButtonHandler
ON STRIG(6) GOSUB JoyButtonHandler
LOCATE 22, 6
PRINT "Press a button on either joystick to see a complete joystick report."
LOCATE 23, 20
PRINT "Press ANY keyboard key to end the program."
'Infinite loop waiting for a Joystick event or keyboard key input.
DO
LOOP WHILE INKEY$ = ""
WrapItUp:
CLS
STRIG(0) OFF
STRIG(2) OFF
STRIG(4) OFF
STRIG(6) OFF
END
JoyButtonHandler:
'Print a label on screen.
LOCATE 10, 14: PRINT "Joystick A"
LOCATE 10, 45: PRINT "Joystick B"
LOCATE 22, 1: PRINT STRING$(80, 32)
DO
'Button 1, Joystick A.
IF STRIG(1) THEN
ButtonStatus$ = "DOWN"
ELSE
ButtonStatus$ = "UP "
END IF
EVENT OFF
LOCATE 16, 10: PRINT "Button 1 is "; ButtonStatus$
EVENT ON
'Button 1, Joystick B.
IF STRIG(3) THEN
ButtonStatus$ = "DOWN"
ELSE
ButtonStatus$ = "UP "
END IF
EVENT OFF
LOCATE 16, 42: PRINT "Button 1 is "; ButtonStatus$
EVENT ON
'Button 2, Joystick A.
IF STRIG(5) THEN
ButtonStatus$ = "DOWN"
ELSE
ButtonStatus$ = "UP "
END IF
EVENT OFF
LOCATE 18, 10: PRINT "Button 2 is "; ButtonStatus$
EVENT ON
'Button 2, Joystick B.
IF STRIG(7) THEN
ButtonStatus$ = "DOWN"
ELSE
ButtonStatus$ = "UP "
END IF
EVENT OFF
LOCATE 18, 42: PRINT "Button 2 is "; ButtonStatus$
EVENT ON
GOSUB UpdateXY
LOOP WHILE INKEY$ = ""
RETURN WrapItUp
UpdateXY:
EVENT OFF
LOCATE 12, 10: PRINT USING "X Position = ###"; STICK(0)
LOCATE 14, 10: PRINT USING "Y Position = ###"; STICK(1)
LOCATE 12, 42: PRINT USING "X Position = ###"; STICK(2)
LOCATE 14, 42: PRINT USING "Y Position = ###"; STICK(3)
EVENT ON
RETURN