Function Keys as Input to Extended Curses


Contents

About this document
    Related documentation
Uprintable or nondisplayable characters
Function keys
Subroutine definitions
Sample program

About this document

This document deals with accepting function keys in an Extended Curses programming environment and has been tested against AIX 3.2.5.

Acquisition of certain key strokes can be problematic because terminals send mutli-byte strings for many of the non-alphanumeric keys. For example, the HFT produces 'ESC' [001q when Function Key 1 is pressed. Adding to the difficulty, the strings produced by the special keys is often different from one terminal type to another. Code development is made much simpler when the Extended Curses library is used to return to an integer value for each special key pressed independent of the terminal type.

This document includes a program which serves to illustrate the use of the Extended Curses library functions to achieve terminal independence and interpretation of multi-byte special key strings into integer values.

Related documentation

InfoExplorer, "General Programming Concepts", Chapter 2, "The Curses Library".

Strang, John. "Programming with curses", O'Reilly & Associates, Inc.


Unprintable or nondisplayable characters

This item contains characters that may not print or display correctly on all systems. A flag line has been placed BENEATH each line that contains one or more of those characters. Flag lines can be identified by the characters "@@" in columns 64 and 65. The position of each unprintable or nondisplayable character is identified by one of the following letters contained in the flag line and located directly beneath that character.

   A - left square bracket          G - logical not
   B - right square bracket         H - exclamation point
   C - left brace                   I - vertical bar
   D - right brace                  J - back slash
   E - percent sign                 K - tilde
   F - caret (circumflex)           L - back quote (accent)

Function Keys

Provided below is a sample program that will accept a function key in the Base, Shift, or Ctrl state and print out the function key pressed. The function keys are recognized as follows:

Function Key   Base   Shift   Ctrl
      F1        F1     F13    F25
      F2        F2     F14    F26
      F3        F3     F15    F27
      F4        F4     F16    F28
      F5        F5     F17    F29
      F6        F6     F18    F30
      F7        F7     F19    F31
      F8        F8     F20    F32
      F9        F9     F21    F33
      F10       F10    F22    F34
      F11       F11    F23    F35
      F12       F12    F24    F36

Subroutine definitions

The sample program uses the following curses subroutines:

   getch()
   csavetty(TRUE)
   extended(FALSE)
   cresetty(FALSE)

Sample program

The following program has been tested with the IBM 3151 Model 310, a vt100 emulator, the HFT, and aixterm. Please note that not all terminals support the Base, Shift, and Ctrl states of function keys (e.g. IBM 3151 does not support Ctrl-Function Keys and VT100 only has F1 - F4 keys).

/********************************************************
 * funkey.c - Prints out the function key pressed.      *
 *            Recognizes F1 - F36:                      *
 *              F1 - F12                                *
 *              Shift F1 - Shift F12 (= f13 - f24)      *
 *              Ctrl F1 - Ctrl F12   (= f25 - f36)      *
 *  To Compile: cc -o funkey.c -0 funkey -lcur          *
 *  To Run: funkey                                      *
 ********************************************************/
#include<cur01.h>
#include<cur02.h>
#define ESC 0x1b
main()  {
        C                                                              @@
  int retval;
  /* --- initialize the curses control structures, etc. */
  initscr();
  /* --- preserve the current state of the tty */
  csavetty(TRUE);
  /* --- turn multibyte processing off */
  extended(FALSE);
  /* --- enable horizontal scrolling */
  scrollok (stdscr,TRUE);
  printw("Enter a function key (ESC to exit)
");
  refresh();
                                             J                         @@
  /* --- get keys - quit on ESCape - display function key value */
      while( (retval = getch()) != ESC ) {
                                H        C                             @@
         if( retval > KEY_F(0) && retval < KEY_F(37) )
             printw("*** F%d ***
",retval - KEY_F(0));
                          E     J                                      @@
         else
             switch(retval ) {
                             C                                         @@
               case KEY_UP: printw("*** Up Arrow ***
"); break;
                                                    J                  @@
               case KEY_DOWN: printw("*** Down Arrow ***
"); break;
                                                        J              @@
               case KEY_LEFT: printw("*** Left Arrow ***
"); break;
                                                        J              @@
               case KEY_RIGHT: printw("*** Right Arrow ***
"); break;
                                                          J            @@
               case KEY_NPAGE: printw("*** Next Page ***
"); break;
                                                        J              @@
               case KEY_PPAGE: printw("*** Previous Page ***
"); break;
                                                            J          @@
               case KEY_HOME: printw("*** Home ***
"); break;
                                                  J                    @@
               case KEY_END: printw("*** End ***
"); break;
                                                J                      @@
               case KEY_DELETE: printw("*** Delete ***
"); break;
                                                      J                @@
               case KEY_IC: printw("*** Insert ***
"); break;
                                                  J                    @@
               default: printw("Some other character
"); break;
                                                    J                  @@
               }
               D                                                       @@
           refresh();
           }
           D                                                           @@
 /* --- reset the tty to its original state */
 cresetty(FALSE);
 endwin();
}
D                                                                      @@



[ Doc Ref: 90605203014666     Publish Date: Nov. 09, 1999]