Microsoft KB Archive/21204

How to Trap the ESC Key and Function Keys with ACCEPT

PSS ID Number: Q21204 Article last modified on 11-16-1992

2.00 2.10 2.20 3.00 3.00a 4.00 4.50 | 3.00 3.00a 4.00 4.50 MS-DOS | OS/2

Question: How can Microsoft COBOL trap function keys and the ESC key?

Response: You can use a format 4 screen ACCEPT followed by a format 1 ACCEPT statement to input the value of a pressed function key, as shown in the example below. Note: A format 4 < ACCEPT screen-name > statement is terminated by pressing the ENTER key on the last screen field, by pressing the ESC key, or by pressing a function key. A format 1 < ACCEPT identifier FROM ESCAPE KEY > statement that follows can then retrieve the value of the function key that was pressed, e.g. F1 returns identifier=2, F2 returns identifier=3, etc. More information about the ACCEPT statement can be found on Pages 185 to 206 of the “Microsoft COBOL Reference Manual” for Versions 2.00, 2.10, and 2.20 for MS-DOS and on Page 3-89 of the “Microsoft COBOL Compiler Version 3.00: Language Reference Manual” for Versions 3.00 and 3.00a.

Code Example 1
The following sample program traps the ESC key and the F1 key (compile with Microsoft COBOL Version 2.00, 2.10, 2.20, 3.00, 3.00a, 4.00, or 4.50):

IDENTIFICATION DIVISION. PROGRAM-ID. FUNCT. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. * * KEY-CODE AND ACCEPT-FIELD ARE DUMMY VARIABLES *  77   KEY-CODE       PIC 99 VALUE 00. 77  ACCEPT-FIELD   PIC X.  * * ESCAPE-CODE HOLDS THE VALUE THAT THE ESC * KEY RETURNS *  77   ESCAPE-CODE    PIC 99 VALUE 01. * * PF-1 HOLDS THE VALUE THAT THE F1 KEY RETURNS *  77   PF-1           PIC 99 VALUE 02. SCREEN SECTION. 01 DUMMY-SCREEN. 03 LINE 1 COLUMN 1 VALUE &quot;PRESS THE KEY OF YOUR CHOICE: &quot;. 03 COLUMN PLUS 1  PIC X  USING ACCEPT-FIELD. PROCEDURE DIVISION. PARA. * * IT IS IMPORTANT TO NOTE THAT THE FULL SCREEN AND * THE ESC AND F1 KEYS ALL RESIDE IN THE SAME * SENTENCE. * * ENTERING ANY ONE OF THE THREE ITEMS (ESC, F1,  * OR ACCEPT-FIELD) WILL SATISFY BOTH ACCEPT * STATEMENTS. WHICH IS TO SAY, ENTERING A CARRIAGE * RETURN AT THE LAST/ONLY FIELD IN DUMMY-SCREEN WILL * TERMINATE THE KEY-CODE ACCEPT, AS WILL ENTERING * ESC OR A DEFINED FUNCTION KEY (F1). *      DISPLAY DUMMY-SCREEN, ACCEPT DUMMY-SCREEN ACCEPT KEY-CODE FROM ESCAPE KEY. IF KEY-CODE = ESCAPE-CODE DISPLAY &quot;ESC&quot; ELSE IF KEY-CODE = PF-1 DISPLAY &quot;PF-1&quot; ELSE EXHIBIT ACCEPT-FIELD. STOP RUN.

Code Example 2
The following is a longer example that traps the ESC key and function keys F1 through F10. This program must be compiled in COBOL Version 3.00, 3.00a, 4.00 or 4.50.

$SET ANS85 * Compile in COBOL 3.00 or 3.00a with the following command: *     COBOL FUNCT.COB; * LINK with the following command: *     LINK FUNCT.OBJ ADIS ADISINIT ADISKEY; *  IDENTIFICATION DIVISION. PROGRAM-ID. FUNCT. ENVIRONMENT DIVISION. DATA DIVISION. WORKING-STORAGE SECTION. * * KEY-CODE AND ACCEPT-FIELD ARE DUMMY VARIABLES * JUST TO HAVE SOMETHING TO ACCEPT. *  77   KEY-CODE       PIC 99 VALUE 00. 77  ACCEPT-FIELD   PIC X.  * LEVEL 78 IS A CONSTANT, LIKE CONST IN BASIC OR #DEFINE IN C.  * THE CONSTANTS BELOW ARE USED TO MAKE THE CODE MORE READABLE, * OR &quot;ENGLISH-LIKE&quot; WHICH IS DESIRABLE FOR COBOL. NOTE THAT * ALL OF THE VALUES BELOW SEEM TO BE &quot;ONE OFF&quot;. THUS F1 HAS * A VALUE OF 2. THIS IS STILL CORRECT. 78  ESCAPE-KEY VALUE 1. 78  F1  VALUE 2. 78  F2  VALUE 3. 78  F3  VALUE 4. 78  F4  VALUE 5. 78  F5  VALUE 6. 78  F6  VALUE 7. 78  F7  VALUE 8. 78  F8  VALUE 9. 78  F9  VALUE 10. 78  F10 VALUE 11. * PF-1 HOLDS THE VALUE THAT THE F1 KEY RETURNS *  SCREEN SECTION. 01 DUMMY-SCREEN AUTO. 03 LINE 9 COLUMN 10 VALUE &quot;PRESS THE KEY OF YOUR CHOICE: &quot;. 03 COLUMN PLUS 1  PIC X  USING ACCEPT-FIELD. 03 LINE 8 COLUMN 10 VALUE &quot;THE ESC KEY STOPS THE PROGRAM&quot;. PROCEDURE DIVISION. PARA. CALL X&quot;E4&quot;. PERFORM ACCEPT-THE-KEY UNTIL KEY-CODE = ESCAPE-KEY. STOP RUN. ACCEPT-THE-KEY. * *    IT IS IMPORTANT TO NOTE THAT THE FULL SCREEN AND *   THE ESC AND F1 KEYS ALL RESIDE IN THE SAME *   SENTENCE. THIS MEANS THAT THE PERIOD COMES AFTER BOTH *   THE DISPLAY AND THE TWO ACCEPTS. *   ENTERING ANY ONE OF THE ITEMS (ESC, F1 - F10,  *    OR ACCEPT-FIELD) WILL SATISFY BOTH ACCEPT *   STATEMENTS. WHICH IS TO SAY, ENTERING A CARRIAGE *   RETURN AT THE LAST/ONLY FIELD IN DUMMY-SCREEN WILL *   TERMINATE THE KEY-CODE ACCEPT, AS WILL ENTERING *   ESC OR A DEFINED FUNCTION KEY (F1 - F10). *      DISPLAY DUMMY-SCREEN, ACCEPT DUMMY-SCREEN ACCEPT KEY-CODE FROM ESCAPE KEY. EVALUATE KEY-CODE WHEN F1 DISPLAY  &quot;THAT WAS THE F1  KEY&quot; AT 1010 WHEN F2 DISPLAY  &quot;THAT WAS THE F2  KEY&quot; AT 1010 WHEN F3 DISPLAY  &quot;THAT WAS THE F3  KEY&quot; AT 1010 WHEN F4 DISPLAY  &quot;THAT WAS THE F4  KEY&quot; AT 1010 WHEN F5 DISPLAY  &quot;THAT WAS THE F5  KEY&quot; AT 1010 WHEN F6 DISPLAY  &quot;THAT WAS THE F6  KEY&quot; AT 1010 WHEN F7 DISPLAY  &quot;THAT WAS THE F7  KEY&quot; AT 1010 WHEN F8 DISPLAY  &quot;THAT WAS THE F8  KEY&quot; AT 1010 WHEN F9 DISPLAY  &quot;THAT WAS THE F9  KEY&quot; AT 1010 WHEN F10 DISPLAY &quot;THAT WAS THE F10 KEY&quot; AT 1010 WHEN ESCAPE-KEY DISPLAY &quot;THAT WAS THE ESC KEY&quot; AT 1010 WHEN OTHER DISPLAY &quot;NOT A FUNCTION KEY  &quot; AT 1010 END-EVALUATE. Copyright Microsoft Corporation 1992.