Wrox Home  
Search P2P Archive for: Go

  Return to Index  

beginning_linux_programming thread: keyboard input, no buffer, no echo


Message #1 by ed orphan <millward@M...> on Fri, 14 Mar 2003 16:47:04 -0600
Back in the good old days, when programming
in DOS using the Borland compiler, I could
use the functions getch and getche for 
non-buffered and no echo keyboard input.
I'm writting a C GNU Linux program and there
isn't any getch or getche functions available.
 getchar isn't any good and neither is getc
because they both use buffered input and
need the user to press the ENTER key before
accepting the input. 
I need a keyboard input function that will take
in one character immediately, without the
user having to press ENTER afterwards.
Is there such a function in C GNU Linux ?
If not, is there any other way to accomplish
this simple task with C GNU Linux?
( I hope its easy. I'm no expert )


Message #2 by millward@m... on Sun, 16 Mar 2003 01:30:16
Amazing what you can find in a good WROX book! 
Sources: Beginning Linux Prgramming, WROX, page 170 
         POSIX Programmer's Guide page 164 
         ISBN 0-937175-73-0 
 #include <termios.h> 
char ky = 'X'; 
struct termios original_t, new_t; 
tcgetattr(fileno(stdin), &original_t); 
new_t = original_t; 
/* got the next line from POSIX */ 
/* turn off CONICAL input and echo */ 
new_t.c_lflag &= ~(ICANON | ECHO); 
printf("\n\t Enter a character > "); 
/* set new attributes */ 
tcsetattr(fileno(stdin), TCSAFLUSH, &new_t); 
fflush(stdin); 
ky = getchar(); 
/* Return to normal */ 
tcsetattr(fileno(stdin), TCSANOW, &original_t); 
  
> Back in the good old days, when programming 
in DOS using the Borland compiler, I could 
use the functions getch and getche for  
non-buffered and no echo keyboard input. 
I'm writting a C GNU Linux program and there 
isn't any getch or getche functions available. 
 getchar isn't any good and neither is getc 
because they both use buffered input and 
need the user to press the ENTER key before 
accepting the input.  
I need a keyboard input function that will take 
in one character immediately, without the 
user having to press ENTER afterwards. 
Is there such a function in C GNU Linux ? 
If not, is there any other way to accomplish 
this simple task with C GNU Linux? 
( I hope its easy. I'm no expert ) 
 
 

  Return to Index