PDA

View Full Version : getche() under linux


azko
07-29-2004, 11:05 AM
Hi
This summer I was so bored that I decided to give a try to Linux c programming and I'm finding some things a little bit hard (surely because I used to program in DOS before). Well, the thing is that I want to get a character on the terminal but without it being printed, like the getche() function did under DOS. I have tried everything and this function seems to be missing in Linux. I also have found that ncurses gives a chance for that, but I want to know if there is a way to simulate getche() without using that library.

thanks
azko[/img]

RobSeace
07-29-2004, 01:31 PM
Well, of course there must be a way; after all ncurses can do it, so
all you need to do is use the same method it does! ;-)

You want to look into tcgetattr() and tcsetattr()... Specifically, you
want to do something like:


int i;
char ch;
struct termios old, new;

tcgetattr (0, &old);
memcpy (&new, &old, sizeof (struct termios));
new.c_lflag &= ~(ICANON | ECHO);
tcsetattr (0, TCSANOW, &new);
i = read (0, &ch, 1);
tcsetattr (0, TCSANOW, &old);
if (i == 1)
/* ch is the character to use */
else
/* there was some problem; complain, return error, whatever */

azko
07-29-2004, 07:14 PM
I have tested it and works perfectly, thanks!