From:	MERC::"uunet!CRVAX.SRI.COM!RELAY-INFO-VAX" 18-MAY-1992 20:41:03.82
To:	Y1Z%EDU.PSU.ECLX@NCDLAB.ULCC.AC.UK, INFO-VAX@KL.SRI.COM
CC:	
Subj:	RE: getch() in VAX C?

In message <6525AD7D2042D449@ecl.psu.edu>, Yang (Y1Z@Edu.PSU.ECLX) writes:

> Hi,
>
> One simple question I cannot make it out:  Is there any command in VAX C
> act as getch() in the Micro-soft C?  That is, without hit the <RETURN> the
> program can directly pass the value of key strok to the viarble.
>
> Any respond will be appreciated.  Thanks.

It is possible by using the Screen Management Routines (SMG). A sample program
is included below by 'signature' and the function you'll be interested in is
GETKEY. You'll need to ensure that smgdef and ssdef are #include'd for the
function to compile if you extract it to another program. Though the function
works, it probably better to create and delete the virtual keyboard once and
leave the function to just read keystrokes. The function here converts the
end-of-file keystroke to Control-Z and, if an error occurs, does not perform a
graceful exit.

David

+-----------------------------------------+--------------------------------+
| David Hardy                             |                                |
| Computer Unit, Newcastle Polytechnic,   | Electronic mail:               |
| Northumberland Building, Ellison Place, |          DAVID.HARDY@NPY.AC.UK |
| Newcastle upon Tyne, NE1 8ST, U.K.      |                                |
+-----------------------------------------+--------------------------------+
| I can't even remember what excuses people gave for their mistakes before |
| there were computers                      --- Frank Clark, King Features |
+--------------------------------------------------------------------------+

#include smgdef		/* Include SMG definitions */
#include ssdef		/* Include System Service message definitions */
#include stdio		/* Include Standard I/O definitions */

main()
{
   /* Local declarations */
   int getkey(), keycode, showmenu;

   /* Initialise */
   showmenu = TRUE;

   /* Loop until F10 is pressed */
   do {

      /* Display a menu only if we need to */
      if (showmenu) {
         printf("\n\n\nMenu\n====\n\n");
         printf("     1   Add record\n");
         printf("     2   Delete record\n");
         printf("     3   Update record\n");
         printf("     4   List records\n\n");
         printf("<Help>   Help\n");
         printf(" <F10>   Exit (If <F10> doesn't work, use Control-Z)\n\n");
         printf("Enter option ");
         showmenu = FALSE;
      }

      /* Wait for a keystroke */
      keycode = getkey();

      /* Act on the keystroke (bleeping those we don't want) */
      switch (keycode) {

         case '1':
            printf("1\n\nAdd record\n");
            showmenu = TRUE;
            break;

         case '2':
            printf("2\n\nDelete record\n");
            showmenu = TRUE;
            break;

         case '3':
            printf("3\n\nUpdate record\n");
            showmenu = TRUE;
            break;

         case '4':
            printf("4\n\nList records\n");
            showmenu = TRUE;
            break;

         case SMG$K_TRM_HELP:
            printf("<Help>\n\nSorry ... No help available\n");
            showmenu = TRUE;
            break;

         case SMG$K_TRM_F10:
         case '\032':
            printf("<F10>\n\nExit requested\n");
            keycode = SMG$K_TRM_F10;	/* in case it was Control-Z */
            break;

         default:
            printf("\007");	/* Bell or beep */
            break;
      }

   } while (keycode != SMG$K_TRM_F10);
}

int getkey()
{
   /* Local declarations */
   long int keyboard, status;
   short int keycode;

   /* Create a virtual keyboard */
   status = SMG$CREATE_VIRTUAL_KEYBOARD(&keyboard);
   if (status != SS$_NORMAL)
      LIB$STOP(status);

   /* Read a keystroke (no echo) */
   status = SMG$READ_KEYSTROKE(&keyboard,&keycode,0,0,0,0,0);
   if (status == SMG$_EOF)
      keycode = '\032';
   else if (status != SS$_NORMAL)
      LIB$STOP(status);

   /* Delete the virtual keyboard */
   status = SMG$DELETE_VIRTUAL_KEYBOARD(&keyboard);
   if (status != SS$_NORMAL)
      LIB$STOP(status);

   /* Return the key code as a long int */
   status = keycode;
   return(status);
}

