Release 10.1A: OpenEdge Development:
Progress 4GL Reference

> ## KEYCODE function

> Evaluates a key label (such as F1) for a key in the predefined set of keyboard keys and returns the corresponding integer key code (such as 301). See OpenEdge Development: Programming Interfaces for a list of key codes and key labels.

> Note: Does not apply to SpeedScript programming.

> #### Syntax

>     KEYCODE ( key-label )

> ``key-label``

> A constant, field name, variable name, or expression that evaluates to a character string that contains a key label. If `key-label` is a constant, enclose it in quotation marks (" ").

> Example

> This procedure displays a menu and highlights different selections on the menu depending on which key you press. On the first iteration of the REPEAT block, the COLOR statement tells Progress to color msg[i] with the same color used to display messages. Because the initial value of i is 1, msg[i] is the first menu selection. Therefore, the first menu selection is colored MESSAGES.

> r-keycod.p
>     DEFINE VARIABLE msg  AS CHARACTER EXTENT 3.
>     DEFINE VARIABLE i    AS INTEGER INITIAL 1.
>     DEFINE VARIABLE newi AS INTEGER INITIAL 1.

>     DISPLAY "     Please choose     " SKIP(1)
>       " 1  Run order entry    " @ msg[1]
>         ATTR-SPACE SKIP
>       " 2  Run receivables    " @ msg[2]
>         ATTR-SPACE SKIP
>       " 3  Exit               " @ msg[3]
>         ATTR-SPACE SKIP
>       WITH CENTERED FRAME menu NO-LABELS.

>     REPEAT:
>         COLOR DISPLAY MESSAGES msg[i] WITH FRAME menu.
>         READKEY.
>         IF LASTKEY = KEYCODE("CURSOR-DOWN") AND i < 3
>         THEN newi = i + 1.
>         ELSE IF LASTKEY = KEYCODE("CURSOR-UP") AND i > 1
>         THEN newi = i - 1.
>         ELSE IF LASTKEY = KEYCODE("GO") OR
>            LASTKEY = KEYCODE("RETURN")
>         THEN LEAVE.

>         IF i <> newi THEN COLOR DISPLAY NORMAL
>           msg[i] WITH FRAME menu.
>         i = newi.
>     END.

> Here’s what happens if you press the cursor-down key:

> 1. The READKEY statement reads the value of the key you pressed.

> 2. The first IF . . . THEN . . . ELSE statement tests to see if the key code of the key you pressed is CURSOR-DOWN. It also checks whether the value of i is less than 3. Both of these things are true, so the procedure adds one to the value of newi, making newi equal two.

> 3. The next two IF statements are ignored because the condition in the first IF statement was true. The procedure continues on the last IF statement: IF i <> newi THEN COLOR DISPLAY NORMAL msg[i] WITH FRAME menu.

> 4. Remember, i is still 1 but newi is now 2. Thus, i is not equal to newi. Which means that the IF statement test is true. Therefore, Progress colors msg[i], which is still msg[1] (the first menu selection), NORMAL. So the first menu selection is no longer highlighted.

> 5. Just before the end of the REPEAT block, i is set equal to newi. Which means that msg[i] is now msg[2], or the second menu selection.

> 6. On the next iteration, the COLOR statement colors msg[i], that is the second menu selection, MESSAGES. The end result of pressing CURSOR-DOWN is that the highlight bar moves to the second menu selection.

> #### See also

> KEYFUNCTION function, KEYLABEL function

Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095