home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / linux / 9485 < prev    next >
Encoding:
Text File  |  1992-08-31  |  1.9 KB  |  71 lines

  1. Newsgroups: comp.os.linux
  2. Path: sparky!uunet!gatech!taco!jlnance
  3. From: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
  4. Subject: Re: Polling keyboard in linux. What works best?
  5. Message-ID: <1992Aug31.124931.8528@ncsu.edu>
  6. Originator: jlnance@volt.ece.ncsu.edu
  7. Keywords: linux
  8. Lines: 56
  9. Sender: news@ncsu.edu (USENET News System)
  10. Reply-To: jlnance@eos.ncsu.edu (JAMES LEWIS NANCE)
  11. Organization: North Carolina State University, Project Eos
  12. References:  <17nf23INNfr8@matt.ksu.ksu.edu>
  13. Date: Mon, 31 Aug 1992 12:49:31 GMT
  14.  
  15.  
  16. In article <17nf23INNfr8@matt.ksu.ksu.edu>, kxb@matt.ksu.ksu.edu (Karl Buck) writes:
  17. |> 
  18. |> I want to check the keyboard for input (using gcc of course) without
  19. |> actually reading (ie: getc etc..). The unix FAQ suggests looking at
  20. |> the FIONREAD ioctl or using select() and the C FAQ states that this is
  21. |> a purely OS dependant question.
  22. |> 
  23. |> What is best for Linux? Can this be non-blocking? A snippet of code
  24. |> for this would be great since I have not had to do this kind of thing
  25. |> before. Thanks.
  26. |> 
  27.  
  28. I was given the following segement of code when I asked this question.  I have
  29. been told that it works on a DECstation, but I have never tested it under 
  30. Linux.
  31.  
  32. Hope it helps,
  33.  
  34. Jim Nance
  35.  
  36. #include    <sgtty.h>
  37. #include    <fcntl.h>
  38.  
  39. struct sgttyb normal_mode, cbreak_mode;
  40.  
  41. set_trap()
  42. {
  43. static int first_time = 1;
  44. if (first_time)
  45.     {
  46.     ioctl(0, TIOCGETP, &normal_mode);
  47.     cbreak_mode = normal_mode;
  48.     cbreak_mode.sg_flags |= CBREAK;    /* make characters available as typed */
  49.     cbreak_mode.sg_flags &= ~ECHO;    /* turn off echo */
  50.     first_time = 0;
  51.     }
  52. ioctl(0, TIOCSETP, &cbreak_mode);
  53. fcntl(0, F_SETFL, FNDELAY);
  54. }
  55.  
  56. unset_trap()
  57. {
  58. fcntl(0, F_SETFL, 0);
  59. ioctl(0, TIOCSETP, &normal_mode);
  60. }
  61.  
  62. chk_buf()    /* read a character from the keyboard buffer */
  63.         /* return '\0' if nothing available */
  64. {
  65. char c;
  66. int status;
  67. status = read(0, &c, 1);
  68. if (status == -1) return(0);
  69. else return(c & 0x7f);
  70. }
  71.