home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16565 < prev    next >
Encoding:
Text File  |  1992-11-15  |  2.8 KB  |  93 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!ferkel.ucsb.edu!taco!rock!stanford.edu!agate!spool.mu.edu!sdd.hp.com!ux1.cso.uiuc.edu!news.cso.uiuc.edu!osiris.cso.uiuc.edu!gordon
  3. From: gordon@osiris.cso.uiuc.edu (John Gordon)
  4. Subject: Re: getch
  5. References: <1992Nov13.022217.13742@pasteur.Berkeley.EDU>
  6. Message-ID: <BxrqH2.KMH@news.cso.uiuc.edu>
  7. Sender: usenet@news.cso.uiuc.edu (Net Noise owner)
  8. Organization: University of Illinois at Urbana
  9. Date: Sun, 15 Nov 1992 17:41:24 GMT
  10. Lines: 81
  11.  
  12. c60c-2gf@web-2a.berkeley.edu () writes:
  13.  
  14.  
  15. >At home, I run a version of Turbo C on my IBM.  There is a library function
  16. >with a prototype in conio.h called getch (and getche).  Is there an 
  17. >equivalent of this function on standard Unix systems.  I'm on a Sun, I 
  18. >believe (not a Sparc station though).  This question may be really
  19. >obvious, but I am not quite sure how all the specifics with input/output
  20. >work on the system (i.e. x-window control etc.) and I can't quite seem
  21. >to find getch.  The man page goes on and on abou'curses,' but I am still
  22. >pretty lost.  Any help would be appreciated (in email, please).  Thanks
  23.  
  24.     The function you want is getchar().  Unfortunately, in the default
  25. mode of most (all?) terminals, getchar() will not return a value until
  26. ENTER has been pressed.  Fortunately, you can change the mode of the
  27. terminal so that it will read 1 character without waiting for ENTER.  (This
  28. is called "raw mode", and the default is "cooked mode", if you care.)
  29.  
  30.     Here is a function that works for me.  It runs under DYNIX/ptx,
  31. which is a POSIX-compliant variant of UNIX SYSV.  If you are running
  32. under a Berkeley-type UNIX, it might need some changing.  Anyway, here's
  33. the function:
  34.  
  35. ------------------------------------------------
  36. /*
  37.    Getkey.c
  38.  
  39.    Written 01/16/1991 by John Gordon, adapted from code supplied by Tim Evans
  40.  
  41.    The purpose of this program is to set ioctl to "raw" mode, read a keypress,
  42.    reset the ioctl mode, and return the character read.
  43.  
  44.    History:
  45.    01/16/91 by John Gordon - initial version adapted from Tim's code
  46. */
  47.  
  48. #include <termio.h>
  49. #include <stdio.h>
  50. #include <string.h>
  51.  
  52. int getkey()
  53. {
  54.   int c;
  55.   struct termio tsav, tchg;
  56.  
  57.   if (ioctl(0, TCGETA, &tsav) == -1)
  58.   {
  59.     perror("getkey: can't get original settings");
  60.     exit(2);
  61.   }
  62.  
  63.   tchg = tsav;
  64.  
  65.   tchg.c_lflag &= ~(ICANON | ECHO);
  66.   tchg.c_cc[VMIN] = 1;
  67.   tchg.c_cc[VTIME] = 0;
  68.  
  69.   if (ioctl(0, TCSETA, &tchg) == -1)
  70.   { 
  71.     perror("getkey: can't initiate new settings");
  72.     exit (3);
  73.   }
  74.  
  75.   c = getchar();
  76.  
  77.   if(ioctl( 0, TCSETA, &tsav) == -1)
  78.   {
  79.     perror("getkey: can't reset original settings");
  80.     exit(4);
  81.   }
  82.  
  83.   return(c);
  84.  
  85. } /* end getkey() */
  86. ------------------------------------------------
  87.  
  88. Enjoy.
  89.  
  90. ---
  91. John Gordon                        My incredibly witty saying has been 
  92. gordon@osiris.cso.uiuc.edu         Politically Corrected into oblivion.
  93.