I am implementing a key reader in c / C ++. I am using linux. I know that the unbuffered getchar function will return small data values ββfor keys. For all ASCII keys (az, AZ, 1-9, punctuation, input, tab, and ESC), one value is returned from getchar (). For other keys, such as the arrow keys, the ESC key will be displayed, but then when getchar () is called again, it will get a different value (A, B, C or D).
A = 65
B = 66
UP arrow = 27 91 65
F5 = 27 91 49 53 126
ESC = 27
full table here
Is there a way to check if there are more characters to read, or is there just one character? When the key is read and the first value is ESC, I do not know if this is a function key starting with ESC or if it is just an ESC key.
#include <stdlib.h> #include <stdio.h> #include <sys/ioctl.h> #include <iostream> #include <string> #include <termios.h> #include <unistd.h> using namespace std; int main(int argc, char *argv[]) { int ch[5]; int i; struct termios term; tcgetattr( STDIN_FILENO, &term ); term.c_lflag &= ~( ICANON | ECHO ); tcsetattr( STDIN_FILENO, TCSANOW, &term ); ch[0] = getchar(); // If ch[0] = 27 and there is more data in the buffer // printf("You pressed a function key"); // Else // printf("You pressed ESC"); return 0; }
source share