home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / comp / lang / c / 18992 < prev    next >
Encoding:
Text File  |  1992-12-29  |  1.8 KB  |  79 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!zaphod.mps.ohio-state.edu!saimiri.primate.wisc.edu!ames!kum.kaist.ac.kr!usenet
  3. From: typhoon@stcon2.kaist.ac.kr (Han Yun-Su)
  4. Subject: Re: Unix equivalent to DOS kbhit() function?
  5. Message-ID: <1992Dec29.073337.2693@kum.kaist.ac.kr>
  6. Sender: usenet@kum.kaist.ac.kr (news)
  7. Organization: KAIST in Seoul, Korea
  8. X-Newsreader: Tin 1.1 PL3
  9. Date: Tue, 29 Dec 92 07:33:37 GMT
  10. Lines: 67
  11.  
  12. I wrote 'kbhit()' function for UNIX.
  13. It's very simple..
  14.  
  15. Here's example source.
  16.  
  17. Save this article as 'kbhit.c', delete headers and signature..
  18. Compile with -lcurses -ltermcap options, that is,
  19.  
  20. cc kbhit.c -o kbhit -lcurses -ltermcap
  21.  
  22. and..type kbhit !
  23. test some characters..
  24. (This works totaly fine for my SUN)
  25.  
  26. Er..sorry.. I'm not good at english..:)
  27. I can speak C more fluently than english..hehe :)
  28. But my major is NOT Computer Science but Life Science. 
  29.  
  30. If you find better idea than this one, Please let me know.
  31.  
  32. Good Luck!
  33.  
  34.  
  35. /* ---- Cut Here ---- */
  36. #include <sgtty.h>
  37. #include <curses.h>
  38.  
  39. kbhit()
  40. {
  41.   static unsigned i;
  42.  
  43.   ioctl(0, FIONREAD, &i);
  44.   return i;
  45. }
  46.  
  47. main()
  48. {
  49.   unsigned long count = 0L;
  50.   int c = ' ';
  51.   int i = 0;
  52.  
  53.   initscr();    /* initialize curses functions */
  54.   crmode();    /* cbreak mode */
  55.   noecho();    /* do not echo pressed character */
  56.  
  57.   mvprintw(0, 0, "Press \'q\' to quit\n");
  58.   refresh();
  59.  
  60.   while (c != 'q') {
  61.     count++;
  62.     if (kbhit()) {
  63.       c = getch();
  64.       mvprintw(1, 0, "%d: \'%c\' is pressed (count: %ld)\n", ++i, c, count);
  65.       refresh();
  66.       }
  67.     }
  68.  
  69.   nocrmode();
  70.   echo();
  71.   endwin();    /* close curses... */
  72. }
  73. /* ---- Cut Here ---- */
  74.  
  75. --
  76. Han Yun-Su, KAIST, Life Science    |   Life is short, DNA is long.
  77. Molecular Genetics Laboratory      |   DNAs, recombine in the neuclus!
  78. Tel: (042)-4061, 4786              |   Slow and steady evolves the life.
  79.