LINUX.ORG.RU

google: getch linux

http://www.c-for-dummies.com/lessons/linux/04/:

About the getch(), Ive tried with the curses way but found it a bit unstylish... I have found a way to do it as follows:

void set_keypress(void) {
struct termios new_settings;
tcgetattr(0,&stored_settings);
new_settings = stored_settings;
new_settings.c_lflag &= (~ICANON);
new_settings.c_cc[VTIME] = 0;
tcgetattr(0,&stored_settings);
nnew_settings.c_cc[VMIN] = 1;
tcsetattr(0,TCSANOW,&new_settings);
}

This takes away the buffering in the Linux terminals so that you don't have to press the Enter key afterwards. You have to include the header file TERMIO.H. And after you run this code you can use the getchar() function and it works just like getch() does in Windows/DOS programs.



To reset you do as follows:



void reset_keypress(void) {
tcsetattr(0,TCSANOW,&stored_settings);
}

phrm ★★
()
Ответ на: комментарий от phrm

спасибо за код (там правда кое-что надо подправить и структурку глобально объявить - а так работает). Еще один вопрос: как выключить эхо при нажатии этого getchar?

anonymous
()
Вы не можете добавлять комментарии в эту тему. Тема перемещена в архив.