ON KEY Trap Fails if CAPS/NUM LOCK Active; Need Separate KEY (21987)






This article was previously published under Q21987

SUMMARY

This article describes a necessary addition to the "Microsoft QuickBasic Compiler" manual for versions 2.0, 2.01, and 3.0. This information is already covered in the documentation for QuickBasic versions 4.0, 4.0b, and 4.5, Microsoft Basic Compiler versions 6.0, 6.0b, and Microsoft Basic Professional Development System (PDS) versions 7.0 and 7.1. The information below also applies to these versions.

Key trapping does not function while the CAPS LOCK key or NUM LOCK key is activated, unless you specifically set up special user-defined keys for that key combination. The program below (and the similar program on page 317 of the "Microsoft QuickBasic Compiler" manual for versions 2.0, 2.01, and 3.0) demonstrates the ignored key trapping when CAPS LOCK or NUM LOCK is active.

MORE INFORMATION

The following program correctly traps CTRL+C when CAPS LOCK and NUM LOCK are NOT active, but does NOT trap CTRL+C if either CAPS LOCK or NUM LOCK (or both) is activated:
   key 15,chr$(&H04)+chr$(&H2E)
   key(15) on
   on key(15) gosub test
   while x$<>chr$(27)
   x$=inkey$
   if x$<>"" the print x$,asc(x$)
   wend
   end
   test:
   print "Control-C has been trapped"
   return
				
This behavior is not a problem in the compiler. The following notes should be added to page 317 of the "Microsoft QuickBasic Compiler" manual for versions 2.0x and 3.0; these notes also apply to QuickBasic versions 4.0, 4.0b, and 4.5, Basic Compiler versions 6.0, 6.0b, and Basic PDS versions 7.0 and 7.1.

To trap the desired key in combination with the CAPS LOCK or NUM LOCK key active, a different keyboard flag must be used for each combination. A value of &H20 hex must be added to the keyboard flag if you want to trap a key while the NUM LOCK key is down. An &H40 must be added to the keyboard flag if you want to trap a key while the CAPS LOCK key is down. For example, a KEY 16 can be added to the above program to trap CTRL+C while the CAPS LOCK key is down, as in the following:
   KEY 16, CHR$(&H44)+CHR$(&H2E)
   ON KEY (16) GOSUB TEST
				
The &H44 above reflects the sum of &H40 (for CAPS LOCK active) and &H04 (the keyboard flag for CTRL).

A KEY 17 can be added to the above program to trap CTRL+C when both CAPS LOCK and NUM LOCK are pressed down, as in the following:
   KEY 17, CHR$(&H64)+CHR$(&H2E)
   ON KEY (17) GOSUB TEST
				
The keyboard flag &H64 above reflects the sum of &H40 (for CAPS LOCK active), &H20 (for NUM LOCK active), and &H04 (the keyboard flag for CTRL).

Modification Type: Minor Last Reviewed: 1/9/2003
Keywords: KB21987