home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / TELECOM / stg_v4.lzh / inkey.c < prev    next >
C/C++ Source or Header  |  1994-11-11  |  2KB  |  111 lines

  1. /* 
  2.  *    inkey        -- get key from keyboard
  3.  *        
  4.  *            INPUT:
  5.  *                iFlag    -- 0=don't wait, 1=wait, -1=reset don't wait mode
  6.  *
  7.  *            OUTPUT:
  8.  *                returns (int) character (0=no key pressed)
  9.  *
  10.  *            ERROR:
  11.  *                (none)
  12.  *
  13.  *            FUNCTIONS CALLED:
  14.  *                function
  15.  *
  16.  *            FUNCTIONS CALLED BY:
  17.  *                function
  18.  *
  19.  *    PROGRAMMER:    Scott t. Griepentrog
  20.  *    DATE started:    91/
  21.  *    VERSION:    0.0
  22.  *    MODIFICATIONS:    91/
  23.  *
  24. */
  25.  
  26. static char *RCSTAG="$Id: inkey.c,v 2.0 1992/08/10 18:48:23 mike Exp $";
  27.  
  28. #include "siv.h"
  29.  
  30. #include <stdio.h>
  31. #include <termio.h>
  32.  
  33. #define ERR (-1)
  34.  
  35. inkey(iFlag)
  36. int iFlag;
  37. {
  38.     struct termio sTermIO;
  39.     static struct termio sTermIOsav;
  40.     static int iNoWait=0;
  41.  
  42.     char ch;
  43.     int i;
  44.  
  45.     if (iFlag==ERR)  /* caller exits before next char pressed */
  46.     {
  47.         if (ioctl(0,TCSETAF,&sTermIOsav)==ERR) exit(perror("ioctl TCSETAF"));
  48.         iNoWait=0;
  49.         return(0);
  50.     }
  51.  
  52.     if (!iFlag && !iNoWait)  /* set for no waiting */
  53.     {
  54.         if (ioctl(0,TCGETA,&sTermIO)==ERR) exit(perror("ioctl TCGETA"));
  55.         if (ioctl(0,TCGETA,&sTermIOsav)==ERR) exit(perror("ioctl TCGETA"));
  56.  
  57.         sTermIO.c_lflag&=~ICANON;
  58.         sTermIO.c_cc[4]=0;    /* minimum # of chars */
  59.         sTermIO.c_cc[5]=0;    /* maximum amt of time */
  60.         sTermIO.c_lflag=0;    /* no echo */
  61.  
  62.         if (ioctl(0,TCSETAF,&sTermIO)==ERR) exit(perror("ioctl TCSETAF"));
  63.         iNoWait=1;
  64.     }
  65.  
  66.     i=read(0,&ch,1);
  67.  
  68.     /* 0=no key pressed */
  69.     if (i==0)
  70.         return(0);
  71.  
  72.     /* ignore any errors */
  73.     if (i==ERR)
  74.         return(0);
  75.  
  76.     if (iNoWait)  /* set back to normal since we have character */
  77.     {
  78.         if (ioctl(0,TCSETAF,&sTermIOsav)==ERR) exit(perror("ioctl TCSETAF"));
  79.         iNoWait=0;
  80.     }
  81.  
  82.     return(ch);
  83. }
  84.  
  85. /*
  86.  * sample test routine
  87. */
  88.  
  89. #ifdef MAIN 
  90. main()
  91. {
  92.     int c;
  93.  
  94.     printf("Press Escape to Quit...\n");
  95.  
  96.     c=0;
  97.     while (c!=27)
  98.     {
  99.         c=inkey(0);
  100.         printf("[%d]\n",c);
  101.  
  102.         /* if got a char, call inkey again right away */
  103.         if (c) continue;
  104.  
  105.         write(1,"\7",1);
  106.         sleep(1);
  107.     }
  108. }
  109.  
  110. #endif
  111.