Microsoft KB Archive/66688

COBOL 4.00 Program to Check SHIFT Key Status Under OS/2

PSS ID Number: Q66688 Article last modified on 11-21-1990

4.00 OS/2

Summary: An COBOL version 4.00 program running under MS OS/2 protected mode can read the state of the SHIFT keys (RIGHT SHIFT, LEFT SHIFT, CTRL, and ALT) and the toggle keys (SCROLL LOCK, NUM LOCK, CAPS LOCK, and INS) by calling the OS/2 API function KbdGetStatus and “CBL_AND”. KbdGetStatus returns information about the keyboard in a buffer parameter and the last word of that parameter is composed of status flags. Each bit of that word reflects the status of one of the above keys. The “CBL_AND” subprogram can then be used to check the status of the keys by examining the state of each bit. A code example that will read and display the states of the toggle keys is shown below. This information applies to Microsoft COBOL Professional Development System (PDS) version 4.00 for MS OS/2.

More Information: As mentioned above, each bit of the last word of the buffer parameter passed to KbdGetStatus reflects the status of the SHIFT or toggle keys. If the bit is set, the key is down (for the SHIFT keys) or on (for the toggle keys). If the bit is cleared, the opposite condition exists (up for the SHIFT keys, off for the toggle keys). The following is a breakdown of the first 8 bits and the keys they represent:

For more information on the API function KbdGetStatus, consult pages 576-577 of “Advanced OS/2 Programming” by Ray Duncan (Microsoft Press, 1989). The following example demonstrates how a protected mode COBOL version 4.00 program can read the status of the toggle keys using the API function KbdGetStatus and “CBL_AND”.

Code Example
SPECIAL-NAMES. CALL-CONVENTION 3 IS OS2API. DATA DIVISION. WORKING-STORAGE SECTION. *  KbdGetStatus buffer parameter. 01 Buffer. 05 BufferSize    PIC 9(4) COMP-5 VALUE 10. 05 KeyboardStat  PIC 9(4) COMP-5. 05 Turnaround    PIC 9(4) COMP-5. 05 InterimFlags  PIC 9(4) COMP-5. *  Receives the status of the SHIFT and toggle keys. 05 SHIFTState    PIC 9(4) COMP-5. *  Receives key-waiting status and next key in keyboard buffer. 01 KeyStat     PIC 99   COMP-X VALUE 0. *  Used to loop through the values for the toggle keys. 01 ToggleKey   PIC 9(4) COMP-5. *  The bits used to compute value of ToggleKey (2**Power). 01 Power       PIC 99   COMP-5. PROCEDURE DIVISION. *   Clear the screen and show a list of the toggle keys. CALL X&quot;E4&quot;. DISPLAY &quot;SCROLL LOCK :&quot; AT 0401. DISPLAY &quot;NUM LOCK   :&quot; AT 0501. DISPLAY &quot;CAPS LOCK  :&quot; AT 0601. DISPLAY &quot;INS    :&quot; AT 0701. *   Loop until the space-bar is hit. PERFORM UNTIL KeyStat = 32 *      Check if a key is waiting in the buffer. CALL X&quot;D9&quot; USING KeyStat *      If a key is waiting, read it. IF KeyStat NOT = 0 THEN CALL X&quot;83&quot; USING KeyStat END-IF *      Read the status of the SHIFT and toggle keys. CALL OS2API &quot;__KbdGetStatus&quot; USING BY REFERENCE Buffer, BY VALUE   0 SIZE 2 *      Loop through the different bits for the toggle keys. PERFORM VARYING Power FROM 4 BY 1 UNTIL Power > 7 *         Compute the bit value for the toggle key. COMPUTE ToggleKey = 2**Power *         Check if the toggle keys bit is set or not. CALL &quot;CBL_AND&quot; USING SHIFTState, ToggleKey BY VALUE 1 *         Change the status of the toggle key accordingly. IF ToggleKey NOT = 0 THEN DISPLAY &quot;ON &quot; AT LINE Power COLUMN 14 ELSE DISPLAY &quot;OFF&quot; AT LINE Power COLUMN 14 END-IF END-PERFORM END-PERFORM. STOP RUN. Copyright Microsoft Corporation 1990.