home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / prgramer / courses / getch.c < prev    next >
C/C++ Source or Header  |  1992-07-09  |  994b  |  47 lines

  1. /*
  2.  * getch()
  3.  *
  4.  * Implimention of the above library call.
  5.  *
  6.  * Replaces the broken implimentation of getch() found in the Watcom C/386 9.0
  7.  * libraries. Note: Watcom's ungetch() probably won't work with my new getch().
  8.  *
  9.  * Copyright July 1992 by G. R. (Fred) Walter
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #define INCL_KBD
  15. #include <os2.h>
  16.  
  17. static char        _getch_flag = 0; 
  18. static KBDKEYINFO *_getch_keyinfo = NULL;
  19.  
  20. int
  21. getch()
  22. {
  23.     if (_getch_keyinfo == NULL) {
  24.         _getch_keyinfo = (KBDKEYINFO *)malloc(sizeof(KBDKEYINFO));
  25.         if (_getch_keyinfo == NULL) {
  26.             fprintf(stderr, "Out of memory error\n");
  27.             exit(1);
  28.         }
  29.     }
  30.             
  31.     if (_getch_flag != 0) {
  32.         _getch_flag = 0;
  33.         return (_getch_keyinfo->chScan);
  34.     }
  35.  
  36.     if (KbdCharIn(_getch_keyinfo, IO_WAIT, 0) != 0)
  37.         return (EOF);
  38.  
  39.     if (_getch_keyinfo->chChar == 0x00 || _getch_keyinfo->chChar == 0xE0) {
  40.         _getch_keyinfo->chChar = 0;
  41.         _getch_flag = 1;
  42.     }
  43.  
  44.     return (_getch_keyinfo->chChar);
  45. }
  46.  
  47.