Microsoft KB Archive/78563

= How to Trap ESC Key in QuickBasic 4.5 and Basic PDS 7.0/7.1 =

Article ID: 78563

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft BASIC Professional Development System 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 &quot;Microsoft Basic 7.0: Language Reference&quot; 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 &quot;press ESC to see trap&quot; PRINT &quot;or Q to quit&quot; DO      ' idle loop LOOP UNTIL UCASE$(INKEY$) = &quot;Q&quot; CALL sub1   '  call sub in main module CALL mod2sub1 PRINT &quot;you are back in main&quot; PRINT &quot;press ESC to see traps still in effect&quot; PRINT &quot;or Q to quit&quot; DO       LOOP UNTIL UCASE$(INKEY$) = &quot;Q&quot;  ' yet another idle loop END

caps:              ' All trap handlers defined here in main PRINT &quot;no caps!&quot;   ' 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 &quot;no num! ' MODULES. RETURN

escapehandle: PRINT &quot;in the escapehandle!!!&quot; RETURN

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

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

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

Additional query words: QuickBas BasicCom 7.00 7.10 4.50

Keywords: KB78563

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.