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

ID: Q71832


The information in this article applies to:


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 query words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50


Keywords          : kb16bitonly 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: July 26, 1999