C getc without CR
I am trying to get input from the keyboard character by character.
I am writing my code in C. The problem with getc and fgetc are that they force the user to enter a carriage return and then all buffered characters appear to be returned one at a time and processed. I want to process them immediatly not waiting for the carriage return key to be struck. I found some code that mimmics the dos command kbhit(). http://www.linux-sxs.org/programming/kbhit.html This code almost works 100%...actually 95%. There are a few bugs. I am using it, and it is working alright. I have to work around a few shortcomings. What is the recommended way to do this with Linux? Astro |
getc and fgetc aren't why buffering happens. Buffering is a file stream attribute.
You can switch to raw mode by doing Code:
struct termios cur,raw; Code:
tcsetattr(STDIN_FILENO,TCSAFLUSH,&cur); |
Thanks alot
Worked great!
Astro |
tuxdev is actually suggesting the same solution you already use with kbhit.
What are the 5% cases where you observe it doesn't work ? |
Quote:
Quote:
Quote:
|
Oh yeah, didn't read that. I guess the difference might be that in this case, the simplicity of using the main library directly causes less errors than complicated mystery code in a library.
Edit: didn't refresh page to see newer comment. I'm not sure if it makes a difference, I pulled that more or less from the code example in Advanced Programming in the Unix Enviroment, originally used in a homework assignment. |
All times are GMT -5. The time now is 02:01 AM. |