home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / misc / volume32 / shlm / part01 / getkey.c < prev    next >
C/C++ Source or Header  |  1992-09-20  |  839b  |  43 lines

  1. /*
  2. **
  3. **    getkey.c
  4. **
  5. **     read a single character from the keyboard and return the ASCII
  6. **    value as a return code
  7. **
  8. **  to compile, type
  9. **
  10. **        cc -O -c getkey.c
  11. **        cc -O -c halt.c
  12. **        cc -o getkey getkey.o halt.o  # add any shared libs here
  13. **
  14. */
  15.  
  16. #include <fcntl.h>
  17. #include <termio.h>
  18.  
  19. int main()
  20. {
  21.     int fd, ch;
  22.     struct termio savedterm, myterm;
  23.     
  24.     if (ioctl( 0, TCGETA, &savedterm ) == -1)
  25.         halt("PERROR getkey:  Can not get terminal driver settings.\n");
  26.  
  27.     myterm = savedterm;
  28.     myterm.c_lflag &= ~(ICANON|ECHO|ISIG);
  29.     myterm.c_cc[VMIN] = 1;
  30.     myterm.c_cc[VTIME] = 0;
  31.  
  32.     if (ioctl( 0, TCSETA, &myterm ) == -1)
  33.         halt("PERROR getkey: Can not set terminal.\n");
  34.  
  35.     if (read(0, &ch, 1) == -1)
  36.             ch = -1;
  37.     else    write(1, &ch, 1);
  38.  
  39.     if (ioctl( 0, TCSETA, &savedterm ) == -1)
  40.         halt("PERROR getkey: Can not set terminal.\n");
  41.     return ch;    
  42. }
  43.