I have a C program that asks the user for their name with a bit of code that reads from stdin on a while loop (until enter is pressed). I am making sure the user can only enter ASCII values from 32 to 126.
The problem is when I press my arrow (cursor) keys, or something like PAGE_DOWN or other... I end up having the ANSI escape sequence printed to the terminal ([A, [6~, etc.).
Here is the section of code.
char name[6];
char c;
uint8_t i = 0;
while ((c = getchar()) != '\n') {
if (c == 127 || c == 8) { // Checks if backspace or del is pressed
i--;
name[i] = ' ';
} else if (c >= 32 && c <= 126) { // Only legal key presses please!
name[i] = c;
i++;
} else {
}
if ((c >= 32 && c <= 126) || c == 127 || c == 8) {
printf_P(PSTR("%c"), c);
}
}
name[5] = '\0';
move_cursor(15, 18);
printf_P(PSTR("%s"), name);
I have certainly chosen to ignore ASCII values outside the 32 to 126 range, so what is the cause of this? Any ideas? Cheers!