Microsoft KB Archive/71832

{|
 * width="100%"|

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

 * }

Q71832

-

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++, versions 1.0, 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 &amp; 64) == 0 ) printf(&quot;The CAPS LOCK key is not on.\n&quot;); 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 &quot;Advanced MS-DOS Programming&quot; 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


 * 1) include 

void main(void);

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

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

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

*flags = (char)(*flags & ~64); } Additional query words: kbinf 6.00 6.00a 6.00ax 7.00 1.00 1.50

Keywords : kb16bitonly

Issue type :

Technology : kbVCsearch kbAudDeveloper kbPTProdChange kbvc150 kbvc100 kbZNotKeyword3 kbCVC700DOS