Here is a little example program I wrote a few years ago.
The program is tested on SCO-Unix, HP-Unix and Linux.

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <termios.h>

/* kleine Unterschiede zwischen Unix-Varianten */
#define SCO
#undef HPUX

/*
 * Lesen von STDIN ohne Blockierung
 */
void main()
{
   int kb;
   struct termios term;
   int omin,otime;

#ifdef SCO
   kb=stdin->__file;   /* File-Descriptor SCO-Unix */
#endif

#ifdef HPUX
   kb=(stdin->__fileH << 8) | stdin->__fileL;  /* File Descriptor HP-Unix */
#endif

   /* Terminal auf Non-Blocking umstellen */
   fcntl(kb,F_SETFL,fcntl(kb,F_GETFL,0)|O_NDELAY);



   /* zusaetzliche Einstellungen ueber TERMIO */
   /* zunaechst alte Werte sichern */
   tcgetattr(kb,&term);

   omin=term.c_cc[VTIME];
   otime=term.c_cc[VMIN];

   /* neue Werte setzen */
   term.c_lflag &= ~ICANON;
   term.c_cc[VTIME]=0;
   term.c_cc[VMIN]=0;
   tcsetattr(kb,TCSANOW,&term);

   /* Schleife bis Taste gedrueckt */
   while (1) {
      fprintf(stdout,"Bitte Taste druecken !!!\n");
      if (getc(stdin) >= 0) break;
   }

   /* alten Zustand wieder herstellen */
   tcgetattr(kb,&term);
   term.c_lflag |= ICANON;
   term.c_cc[VTIME]=otime;
   term.c_cc[VMIN]=omin;
   tcsetattr(kb,TCSANOW,&term);
   exit(0);
}
/* end main() */



Hermann Betz

http://www.yellowstone-soft.de
[EMAIL PROTECTED]

maixy schrieb:

> Hi all,
>   I want to get the character while I strike any key,I try to use getchar(),but it 
>need to strike ENTER to end of string,can I get the value of key immediately before I 
>strike ENTER.In DOS,I can use kbhit() to check if there is any key to be striked,but 
>how can I do in linux?
>   I have no idea how to get the key. Do you have any idea?
>
> Thanks!
>
> Michael
>
> -- [rtl] ---
> To unsubscribe:
> echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
> echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
> --
> For more information on Real-Time Linux see:
> http://www.rtlinux.org/rtlinux/

-- [rtl] ---
To unsubscribe:
echo "unsubscribe rtl" | mail [EMAIL PROTECTED] OR
echo "unsubscribe rtl <Your_email>" | mail [EMAIL PROTECTED]
--
For more information on Real-Time Linux see:
http://www.rtlinux.org/rtlinux/

Reply via email to