Reputation: 5904
How can I generate a "readable" backspace in command prompt?
I have a tiny C app and I'm trying to read a backspace from input with getchar() method.
Is there any key combination to trigger this (and still being able to capture it)? (Similar to Ctrl-Z to trigger an EOF)
Upvotes: 1
Views: 1007
Reputation: 459
Ascii code of backspace button is 127, so every function returns an integer from terminal input will probably return 127 in case of backspace. Be careful because it is the same code for delete button.
Linux
See this answer first and implement your own getch() then follow Windows code example (obviously without conio.h lib).
Windows
Just add #include<conio.h>
in the beginning of your source file.
This is my SSCCE, just need to write something like this
int main(void) {
int c;
while (1) {
c = getch();
if (c == 127) {
printf("you hit backspace \n");
} else {
printf("you hit the other key\n");
}
}
return 0;
}
Upvotes: 1
Reputation: 16607
You can use curses function getch()
to read backspace.
Chech the following link -
how to check for the "backspace" character in C
In the above link in answer ascii value of backspace is used to read it .
Upvotes: 1
Reputation: 609
Backspace is special! Normally you will need to use some raw/unbuffered keyboard I/O mode to capture it.
On Windows you may want to try using getch
instead of getchar
. See also: What is the difference between getch() and getchar()?
Upvotes: 1