Q(uick)BASIC Statements: ON event

Quick View

ON event

Event trapping statements that indicate the first line of an event-trapping subroutine

Worth knowing

Useful and cross-version information about the programming environments of QBasic and QuickBasic.

Syntax
  • ON event GOSUB {linenumber | linelabel}
Description/Parameter(s)
Argument Description
event A keyword that specifies the event that causes a branch to the event-trapping subroutine. An event is a condition on a specific device:
COMcharacters received at a communications port
KEYa designated keystroke
PENlightpen activated
PLAYtoo few notes in background music queue
STRIGjoystick trigger pressed
TIMERtime interval has elapsed
linenumber or linelabel The number or label of the first line in the event- trapping subroutine. This line must be in the module- level code.
Note: A linenumber of 0 disables event trapping and does not specify line 0 as the start of the subroutine.

The ON event statement only specifies the start of an event-trapping subroutine. Another set of statements determines whether or not the subroutine is called. This set of statements turns event trapping on or off and determines how events are handled when trapping is off. The following list describes these statements in a general way.

Event Description
event ON Enables event trapping. Event trapping occurs only after an event ON statement is executed.
event OFF Disables event trapping. No trapping takes place until the execution of another event ON statement. Events occurring while trapping is off are ignored.
event STOP Inhibits event trapping so no trapping takes place until an event ON statement is executed. Events occurring while trapping is inhibited are remembered and processed when an event ON statement is executed.

For specific information about each of these statements, see the

When an event trap occurs (the subroutine is called), BASIC performs an automatic event STOP that prevents recursive traps. The RETURN from the trapping subroutine automatically performs an event ON statement unless an explicit event OFF is performed inside the subroutine.

Note: Because of the implicit event STOP and event ON statements, the events during execution of the trapping subroutine are remembered and processed when the trapping subroutine ends.

The RETURN linenumber or RETURN linelabel forms of RETURN can be used to return to a specific line number from the trapping subroutine. Use this type of return with care, however, because

  • any other GOSUB, WHILE, or FOR statements active at the time of the trap remain active. This may produce error messages such as "NEXT without FOR."
  • if an event occurs in a procedure, a RETURN linenumber or RETURN linelabel statement cannot get back into the procedure because the line number or label must be in the module-level code.

Differences from BASICA

  • If you use BC from the DOS prompt and a program contains ON event statements, you must use /V or /W. These options allow the compiler to function correctly when event-trapping subroutines are included in a program. BASICA does not require additional options.
Example

The following programming examples show how to integrate ON event statements, statements that enable traps, and event-handling subroutines into a working program: ON PLAY(n) statement programming example
ON TIMER(n) statement programming example

Description/Parameter(s)

The ON event statements are used to trap events. For further information, see ON COM, ON KEY, ON PEN, ON PLAY, ON SIGNAL, ON STRIG, ON TIMER, or ON UEVENT:

Specifies a trap Type of event Enables the trap Available in DOS or OS/2 protected mode
ON COM(n) Events on a communications port COM(n) ON DOS & OS/2
ON KEY(n) A keystroke KEY(n) ON DOS & OS/2
ON PEN Lightpen events PEN ON DOS only
ON PLAY(n) Fewer than queluelimit% notes in the background-music queue PLAY ON DOS only
ON SIGNAL An OS/2 signal event SIGNAL OS/2 only
ON STRIG(n) Events on a joystick trigger STRIG(n) ON DOS only
ON TIMER(n) Timer events TIMER ON DOS & OS/2
ON UEVENT User-defined events UEVENT ON DOS & OS/2