I want to read every keystroke from a console application written in c under windows and linux immediately. Unfortunately the function gets(line) does only return a value, when
The following code worked for me. Thank you for pointing me into to right direction. http://bytes.com/topic/c/answers/503640-getch-linux
#include <termios.h>
#include <unistd.h>
int mygetch(void)
{
struct termios oldt,
newt;
int ch;
tcgetattr( STDIN_FILENO, &oldt );
newt = oldt;
newt.c_lflag &= ~( ICANON | ECHO );
tcsetattr( STDIN_FILENO, TCSANOW, &newt );
ch = getchar();
tcsetattr( STDIN_FILENO, TCSANOW, &oldt );
return ch;
}
You're probably looking for getch()
. On Windows (at least VC++) it's declared in <conio.h>
. On Linux it's part of curses.
I think you're looking for getchar()
and putchar()
:
#include <stdio.h>
char line[MAX_LINE];
int i = 0;
int c;
while ( (c = putchar(getchar())) != EOF)
{
line[i] =c;
}