How to Disable Toggle for NUM/CAPS/SCROLL LOCK with Key Trap

ID: Q59132


The information in this article applies to:
  • Microsoft Visual Basic Standard and Professional Editions for MS-DOS, version 1.0
  • Microsoft QuickBASIC for MS-DOS, versions 4.0, 4.0b, 4.5
  • Microsoft BASIC Compiler for MS-DOS and MS OS/2, versions 6.0, 6.0b
  • Microsoft BASIC Professional Development System (PDS) for MS-DOS, version 7.0


SUMMARY

By setting up key traps for the NUM LOCK, CAPS LOCK, and SCROLL LOCK keys, a Basic program can prevent the toggling of each of these key states. This is extremely useful for trapping other keys because the program can limit the number of keys to be defined. The first example below demonstrates this type of functionality.

Another example listed below demonstrates how to programmatically (without pressing the key) force the CAPS LOCK state to ON or OFF. This same example can be used for the NUM LOCK and SCROLL LOCK keys.


MORE INFORMATION

The syntax of the KEY statement (where n=15 through 25 can be user-defined keys) is as follows:


   KEY n, CHR$(keyboardflag) + CHR$(scancode) 
The following are the scan codes are for the CAPS LOCK, NUM LOCK, and SCROLL LOCK keys:

   Key           Scan Code
   ---           ---------
   CAPS LOCK     &H3A
   NUM LOCK      &H45
   SCROLL LOCK   &H46 
By defining traps for NUM LOCK and CAPS LOCK, your program can effectively reduce (by a factor of four) the number of keys needed to be defined. For example, to trap both the CTRL+ALT+DEL and CTRL+BREAK keystroke combinations, 16 keys must be created to handle all the different keystroke combinations. This is more than the maximum number of user-defined keys (11). By defining CAPS and NUM LOCK traps, only 4 additional keys must be defined.

Code Example

The following code example disables the toggles for the CAPS LOCK, NUM LOCK, and SCROLL LOCK keys:

' NOTE: This program assumes NUM LOCK and CAPS LOCK are off at the
'       beginning. See the article referenced above for how to set
'       the NUM and CAPS LOCK off.
KEY 15, CHR$(&H0) + CHR$(&H3A)       ' CAPS LOCK
ON KEY(15) GOSUB caps
KEY(15) ON

KEY 16, CHR$(&H0) + CHR$(&H45)       ' NUM LOCK
ON KEY(16) GOSUB num
KEY(16) ON

KEY 17, CHR$(&H0) + CHR$(&H46)       ' SCROLL LOCK
ON KEY(17) GOSUB scroll
KEY(17) ON

WHILE INKEY$ <> CHR$(27): WEND
END

caps: PRINT "NO CAPS LOCK TOGGLE!"
RETURN

num:  PRINT "NO NUM LOCK TOGGLE!"
RETURN

scroll:  PRINT "NO SCROLL LOCK TOGGLE!"
RETURN 
The code example below allows you to determine the state of the CAPS LOCK key with the following PEEK command:

   DEF SEG = 0
   X = PEEK(1047) AND 64 
If CAPS LOCK is off, the value of "X" will be "0". If CAPS LOCK is on, the value will be 64.

To force the CAPS LOCK off, use the following command:

   DEF SEG = 0
   POKE 1047, PEEK(1047) AND 191 
To force the CAPS LOCK on, use the following:

   DEF SEG = 0
   POKE 1047, PEEK(1047) OR 64 
Each bit of location 1047 reflects the status of a keyboard flag. This includes NUM LOCK, SCROLL LOCK, CAPS LOCK, INSert mode, and whether or not the LEFT SHIFT and RIGHT SHIFT keys, the ALT key, or the CTRL (Control) key is currently pressed or not. See the following table for more information:

   Bit No.         Decimal Value           Keyboard Flag
   ---------------------------------
   0               1                       RIGHT SHIFT
   1               2                       LEFT SHIFT
   2               4                       CTRL (Control)
   3               8                       ALT
   4               16                      SCROLL LOCK
   5               32                      NUM LOCK
   6               64                      CAPS LOCK
   7               128                     INS (Insert mode) 
Although the INS key is included in the table shown above, you cannot make use of it. In Visual Basic for MS-DOS, you cannot use either the POKE or the CALL INTERRUPT to set the Insert mode when you are in Forms mode. The only way to set the Insert mode is by pressing the INS key. This is by design in Visual Basic for MS-DOS.

To determine the state of any flag, the following statement will return "0" if the flag is clear (off or not pressed), and will return <bval> if the flag is set (on or pressed):

   PEEK(1047) AND <bval>
   (where <bval> is the decimal value of the bit that represents
   the flag you want) 
To force the flag on (this applies only to the LOCK keys -- not the INS key), you need to set the appropriate bit. You can do this with the following POKE statement:

   POKE 1047, PEEK(1047) OR &lt;bval&gt;
   (where <bval> is the decimal value of the flag you want to set) 
To force the flag off, you can use the following similar statement:

   POKE 1047, PEEK(1047) AND (255 - <bval>) 
Note that simply poking the bit value into 1047 would effectively set the flag, but would also clear all other flags. Thus, be sure to retain the previous values of the other flags by using the above strategies.

Note that instead of using PEEK, you may also get the status of keyboard flags with IBM ROM BIOS Interrupt 16h, using function number 2. This interrupt returns the ROM BIOS flags byte that describes the state of the following keyboard toggles and SHIFT keys: RIGHT SHIFT or LEFT SHIFT key down, CTRL key down, ALT key down, SCROLL LOCK on, NUM LOCK on, CAPS LOCK on, INSert on.

Additional query words: VBmsdos QuickBas BasicCom 1.00 4.00 4.00b 4.50 6.00 6.00b 7.00 SCROLLLOCK NUMLOCK CAPSLOCK

Keywords :
Version : MS-DOS:1.0,4.0,4.0b,4.5; :6.0,6.0b,7.0
Platform : MS-DOS
Issue type :


Last Reviewed: December 4, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.