How to Modify the Keyboard-Flags Byte from a C Program

Last reviewed: July 17, 1997
Article ID: Q71832
6.00 6.00a 6.00ax 7.00 | 1.00 1.50
MS-DOS                 | WINDOWS
kbprg

The information in this article applies to:

  • Microsoft C for MS-DOS, versions 6.0, 6.0a, and 6.0ax
  • Microsoft C/C++ for MS-DOS, version 7.0
  • Microsoft Visual C++ for Windows, versions 1.0 and 1.5

SUMMARY

The following information, which may be used with Microsoft C and QuickC, describes how to access the keyboard-flags byte in the ROM BIOS data area from a C program. This procedure allows a keyboard key, such as INS (insert) or CAPS LOCK, to be set or cleared from within a program. Note that the methods described below can be used to access any particular memory location (not just the keyboard flags).

MORE INFORMATION

At 0000:0417H in the ROM BIOS data area there is a byte that contains information pertaining to the status of several keyboard keys. The following table lists each bit and its meaning when set:

   Bit No.   Decimal Value      Meaning If Set
   -------   -------------      --------------

     0            1             Right SHIFT key depressed
     1            2             Left SHIFT key depressed
     2            4             CTRL key depressed
     3            8             ALT key depressed
     4           16             SCROLL LOCK on
     5           32             NUM LOCK on
     6           64             CAPS LOCK on
     7          128             INS on

The first step in reading or modifying this data from a C program is to declare a pointer to the byte that contains the keyboard flags. For example:

   char far * flags = (char far *)0x00000417L;

Once you have a pointer to the correct address, you can determine if a particular flag is set by doing a bitwise-and with the decimal value of the bit that represents the flag. For example, consider the following conditional expression:

   if ( (*flags & 64) == 0 )
        printf("The CAPS LOCK key is not on.\n");

If the CAPS LOCK key is on, the result of the bitwise-and is 64.

To turn off a particular flag, you must do a bitwise-and with the one's complement of the flag's value (that is, ~flag). For example, you can turn off the CAPS LOCK key as follows:

   *flags = (char)(*flags & ~64);

To turn on a particular flag, perform a bitwise-or with the decimal bit value of the flags you want to turn on. For example, the following is an example of turning on the CAPS LOCK key:

   *flags = (char)(*flags | 64);

Much of this information is outlined in the Microsoft Press book "Advanced MS-DOS Programming" by Ray Duncan.

The following sample program turns on the CAPS LOCK key and prompts for some keyboard entry to demonstrate that CAPS LOCK is really set.

Sample Code

/* Compile options needed: none
*/

#include <stdio.h>

void main(void);

void main()
{
   char far *flags = (char far *)0x00000417L;
   char string[10];

   *flags = (char)((*flags) | 64);

   printf("\nType something, it should be in CAPS: ");
   gets(string);

   *flags = (char)(*flags & ~64);
}


Additional reference words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50
KBCategory: kbprg
KBSubcategory: CLngIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 17, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.