home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / snip9707.zip / TIMEGETC.C < prev    next >
C/C++ Source or Header  |  1997-07-05  |  899b  |  50 lines

  1. /* +++Date last modified: 05-Jul-1997 */
  2.  
  3. /*
  4. **  TIMEGETC.C - waits for a given number of seconds for the user to press
  5. **               a key.  Returns the key pressed, or EOF if time expires
  6. **
  7. **  by Bob Jarvis
  8. */
  9.  
  10. #include <stdio.h>
  11. #include <time.h>
  12. #include <conio.h>
  13. #include "snipkbio.h"
  14.  
  15.  
  16. int timed_getch(int n_seconds)
  17. {
  18.       time_t start, now;
  19.  
  20.       start = time(NULL);
  21.       now = start;
  22.  
  23.       while(difftime(now, start) < (double)n_seconds && !kbhit())
  24.       {
  25.             now = time(NULL);
  26.       }
  27.  
  28.       if(kbhit())
  29.             return getch();
  30.       else  return EOF;
  31. }
  32.  
  33. #ifdef TEST
  34.  
  35. main()
  36. {
  37.       int c;
  38.  
  39.       printf("Starting a 5 second delay...\n");
  40.  
  41.       c = timed_getch(5);
  42.  
  43.       if(c == EOF)
  44.             printf("Timer expired\n");
  45.       else  printf("Key was pressed, c = '%c'\n", c);
  46.       return 0;
  47. }
  48.  
  49. #endif /* TEST */
  50.