Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

read one character from stdin

83 views
Skip to first unread message

Mark

unread,
May 9, 2003, 3:27:18 AM5/9/03
to
Hi,

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


Clemens Kirchgatterer

unread,
May 9, 2003, 3:33:57 AM5/9/03
to
On Fri, 09 May 2003 09:27:18 +0200, Mark wrote:

> 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);
}

Harald Grossauer

unread,
May 9, 2003, 3:41:05 AM5/9/03
to
Mark wrote:

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.

Mark

unread,
May 9, 2003, 3:57:18 AM5/9/03
to

"Clemens Kirchgatterer" <cle...@thf.ath.cx> wrote in message
news:pan.2003.05.09.07...@thf.ath.cx...

Ok thanks, that did the trick


Kasper Dupont

unread,
May 9, 2003, 10:18:56 AM5/9/03
to
Mark wrote:
>
> Hi,
>
> 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.

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(_);

0 new messages