Chapter 9. Event Polling and Error/Event Trapping
133
9.4 Event (of Keystroke) Trapping
[ 1 ] Overview
If any of the function keys previously specified for keystroke trapping is pressed, event
trapping makes the program cause an interrupt so as to transfer control from the current
program to the specified event-handling routine.
This trapping facility checks whether any of the function keys is pressed or not between every
execution of the statements.
[ 2 ] Programming for trapping keystrokes
To trap keystrokes, use both the ON KEY...GOSUB and KEY ON statements. The ON
KEY...GOSUB statement designates the key number of the function key to be trapped and
the event-handling routine (to which control is to be transferred if a specified function key is
pressed) in its label. The KEY ON statement activates the designated function key.
This trapping cannot take effect until both the ON KEY...GOSUB and KEY ON statements have
been executed.
The keystroke of an unspecified function key or any of the numerical keys cannot be trapped.
The following program sample will trap keystroke of magic keys M1 and M2 (these keys are
numbered 30 and 31, respectively).
sub1
sub2
ON KEY (30)GOSUB sub1
ON KEY (31)GOSUB sub2
KEY (30)ON
KEY (31)ON
:
:
(Main routine)
:
:
END
(Event-handling routine 1)
RETURN
(Event-handling routine 2)
RETURN
The RETURN statement in the event-handling routine will return control to the statement
immediately following that statement where the keyboard interrupt occurred.
Even if a function key is assigned a null string by the KEY statement, pressing the function key
will cause a keyboard interrupt when the KEY ON statement activates that function key.
Kommentare zu diesen Handbüchern