How to Trap ESC Key in QuickBasic 4.5 and Basic PDS 7.0/7.1 (78563)



The information in this article applies to:

  • Microsoft QuickBASIC 4.5
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.0
  • Microsoft Basic Professional Development System (PDS) for MS-DOS and MS OS/2 7.1

This article was previously published under Q78563

SUMMARY

This article describes how to trap the ESC key, allowing for different states of NUM LOCK and CAPS LOCK during the program's execution.

The example below also illustrates how event handlers must be at the module level, and how RETURN statements will resume execution at the appropriate SUB (or FUNCTION). In a multi-module program, traps activated in the main module are global to the program.

This information applies to Microsoft QuickBasic version 4.5 for MS-DOS and to Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1 for MS-DOS and MS OS/2.

MORE INFORMATION

A difference between error and event trapping is that if an event occurs in a module other than the main module, execution returns (using a RETURN statement) to the SUB in which the event occurred. However, for error trapping, control returns (with the RESUME statement) to the point in the main program following the call to the SUB in which the error occurred (or to the level of the closest nested error handler, if any, for the nested subprograms).

There are two different ways to handle the many possible keyboard states: one is to exhaustively define all possible combinations using the KEY statement. This is limited by the 11 possible user-defined keys available. The second method is to set a generic trap for the CAPS LOCK and NUM LOCK keys that prevents them from being toggled on, and then defining a single trap for the ESC key.

Both methods are illustrated below. The generic trap method assumes that the CAPS LOCK and NUM LOCK keys are off at the start of the program. If CAPS LOCK and NUM LOCK are not off, you can ask the user to turn them off, or you can force these keys off, as described in a separate article, which can be found by querying on the following words:

Force and Caps and Lock and Peek and Poke

Note: For a discussion of defining combinations of keyboard states, see pages 179-182 of the "Microsoft Basic 7.0: Language Reference" for Basic PDS versions 7.0 and 7.1.

Code Example

' REMEMBER: FOR GENERIC METHOD, AT START OF PROGRAM, ALL LOCK KEYS
' MUST BE OFF.

'Main module

' INFORM THE USER TO UNTOGGLE CAPS LOCK and NUM LOCK OR FORCE THEM OFF
' ONCE AND THE TRAP WILL INTERCEPT THEM THROUGHOUT THE PROGRAM.
' '>>> TO TEST THE EXHAUSTIVE METHOD, COMMENT OUT THE GENERIC DEFINE
' FOR THE ESCAPE KEY AND HAVE THE LOCK KEYS TOGGLED ON GOING INTO THE
' PROGRAM RUN. THE ESCAPE TRAP WILL STILL WORK.

DECLARE SUB mod2sub1 ()

KEY 19, CHR$(32) + CHR$(1)   'Escape with NUM LOCK -THE EXHAUSTIVE
                             'METHOD
KEY 20, CHR$(64) + CHR$(1)   'Escape with CAPS LOCK -THE EXHAUSTIVE
                             'METHOD
KEY 21, CHR$(96) + CHR$(1)   'Escape with both NUM LOCK + CAPS LOCK -
                             'THE EXHAUSTIVE METHOD
KEY 22, CHR$(0) + CHR$(1)   'Escape with no other key - THE GENERIC
                            'METHOD
KEY 23, CHR$(0) + CHR$(58)   'caps     - NEED THIS FOR GENERIC METHOD
KEY 24, CHR$(0) + CHR$(69)   'num      - NEED THIS FOR GENERIC METHOD

ON KEY(19) GOSUB escapehandle   ' turn on all traps except for two
ON KEY(20) GOSUB escapehandle   ' DEL keys at the main module level
ON KEY(21) GOSUB escapehandle
ON KEY(22) GOSUB escapehandle
ON KEY(23) GOSUB caps
ON KEY(24) GOSUB num

KEY(19) ON       'turn on all key definitions but NOT the trap itself
KEY(20) ON
KEY(21) ON
KEY(22) ON
KEY(23) ON
KEY(24) ON

CLS
PRINT "press ESC to see trap"
PRINT "or Q to quit"
        DO       ' idle loop
        LOOP UNTIL UCASE$(INKEY$) = "Q"
CALL  sub1   '  call sub in main module
CALL mod2sub1
PRINT "you are back in main"
PRINT "press ESC to see traps still in effect"
PRINT "or Q to quit"
        DO
        LOOP UNTIL UCASE$(INKEY$) = "Q"  ' yet another idle loop
END

caps:               ' All trap handlers defined here in main
PRINT "no caps!"    ' except for two DEL keys handlers
RETURN              ' which will be activated in module 2 or 3

num:                ' AND WILL REMAIN ACTIVE AFTER RETURN FROM THOSE
PRINT "no num!      ' MODULES.
RETURN

escapehandle:
PRINT "in the escapehandle!!!"
RETURN

SUB sub1
PRINT " in the sub1"
        DO
        LOOP UNTIL UCASE$(INKEY$) = "Q"
END SUB

'Second module - use 'Create File' to create second module in the
'QB(X).EXE environment.

SUB mod2sub1
PRINT " in the mod2sub1"
        DO
        LOOP UNTIL UCASE$(INKEY$) = "Q"
END SUB
				

Modification Type:MinorLast Reviewed:1/8/2003
Keywords:KB78563