I am making a very simple application which needs some instructions from the
user. These instructions are single keypresses without a following enter. So
actually I just need to wait for one input. In windows there is a function
called getch() for which you need to include conio.h. Is there a similar
function in linux? I tried getchar(), but that does need a return at the
end. It will then process all characters if u call getchar() enough times.
This is not the desired behaviour.
Can anyone help me out here?
thanks in advance,
Mark
> These instructions are single keypresses without a following
> enter. So actually I just need to wait for one input.
#include <termios.h> // struct termios
#include <unistd.h> // read(), usleep()
#define STDIN 0
static struct termios initial_settings;
int
term_nonblock(void) {
struct termios new_settings;
if (tcgetattr(STDIN, &initial_settings) < 0) return (-1);
new_settings = initial_settings;
// XXX Ctrl-C is disabled!
new_settings.c_lflag &= ~(ICANON | ECHO | ISIG);
new_settings.c_cc[VMIN] = 0;
new_settings.c_cc[VTIME] = 0;
if (tcsetattr(STDIN, TCSANOW, &new_settings) < 0) return (-2);
return (0);
}
int
term_reset(void) {
if (tcsetattr(STDIN, TCSANOW, &initial_settings) < 0) return (-1);
return (0);
}
char
term_waitkey(void) {
char ch = 0;
int nread = 0;
while (1) {
nread = read(STDIN, &ch, 1);
if (nread > 0) break;
usleep(10000);
}
return (ch);
}
IIRC stdin is line buffered, i.e. data is sent through the stream only after
the user enters a return. Have a look at "man setbuf", maybe setting stdin
to "_IONBF" helps.
Ok thanks, that did the trick
http://www.daimi.au.dk/~kasperd/comp.os.linux.development.faq.html#termios
--
Kasper Dupont -- der bruger for meget tid på usenet.
For sending spam use mailto:aaa...@daimi.au.dk
for(_=52;_;(_%5)||(_/=5),(_%5)&&(_-=2))putchar(_);