Last active
April 8, 2022 03:28
-
-
Save lesovsky/f0f5d31ac48f2fc53e5f to your computer and use it in GitHub Desktop.
Get key code with ncurses.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ncurses.h> | |
int main() | |
{ | |
initscr(); | |
keypad(stdscr, true); //Включаем режим чтения функциональных клавиш | |
noecho(); //Выключаем отображение вводимых символов, нужно для getch() | |
halfdelay(100); //Устанавливаем ограничение по времени ожидания getch() в 10 сек | |
printw("Press F2 to exit.\n"); | |
bool ex = false; | |
while ( !ex ) | |
{ | |
int ch = getch(); | |
switch ( ch ) | |
{ | |
case ERR: | |
printw("Please, press any key...\n"); //Если нажатия не было, напоминаем пользователю, что надо нажать клавишу | |
break; | |
case KEY_F(2): //Выходим из программы, если была нажата F2 | |
ex = true; | |
break; | |
default: //Если всё нормально, выводим код нажатой клавиши | |
printw("Code of pressed key is %d\n", ch); | |
break; | |
} | |
refresh(); //Выводим на настоящий экран | |
} | |
printw("Thank you. Good buy!"); | |
getch(); | |
endwin(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Спасибо!