[LCP]Unbuffered getchar()

McDonald, Joshua J (Josh) joshmcdonald at lucent.com
Fri Apr 11 10:35:02 UTC 2003


Hi All,

I'm new to this list and was wondering how active it is (I haven't seen any
mailings at all in a day and a half)?

Also, has anyone had much experience getting unbuffered keyboard input using
getchar()?
I've done a little looking, and found that if I use something similar to the
following,  that I can read each keystroke in as it is pressed, rather than
waiting for a return:

#include <stdio.h>
#include <termios.h>
#include <sys/ioctl.h>

main()
{
struct termios ts;
struct termios new_ts;
char c;
ioctl(0, TCGETS, &ts);
new_ts = ts;
new_ts.c_lflag &= !ICANON;
new_ts.c_lflag &= !ECHO;
ioctl(0, TCSETS, &new_ts);

c=getchar();
printf("%c\n",c);
}

I'm using "gcc -o test test.c" to compile, on a RH8.0 system.
Once the program exits, however, I need to use "stty sane" to get bash (Or
whatever) to echo characters (and linefeeds, etc) to the screen once again.

Does anyone know enough about termios to instruct me how to have the program
automatically return to a state where input is buffered/echo'd again?

Thanks in advance,
Josh.


Note: This email (and/or its attachments) contains information belonging to
the sender, which may be confidential, proprietary, and/or legally
privileged.  The information is intended only for the use of the
individual/s or entity/ies named above.  If you are not the intended
recipient, you are hereby notified that any disclosure, distribution or
taking of any action in reliance on the content of this is strictly
forbidden.  If you have received this e-mail in error please immediately
notify the sender identified above. 




More information about the linuxCprogramming mailing list