I'm working on multiple C programs like a shell and a text editor that require to be run without the ECHO and ICANON flags. I disabled these using termios.h and managed to write my own gets function that can relay returned strings to my program and do special things for escape characters. The only think I can't do is print a backspace. If, for example, I use this code:
void mgets(char *str)
{
int c, i = 0;
while((c = getchar()) != '\n')
if(c == 27)
// the user hit ESC, ignore it for now
else if(c == '\b')
puts("\b \b") // this is where it SHOULD backspace
// else if it's a regular character:
else {
str+i = c; i++; // write that to the string...
putchar(c); // ...and echo it to the screen
}
}
It all works great but the program just doesn't respond when I backspace. if I change my if statement a bit...
if(c == '\b')
printf("You hit a backspace!");
But it's still unresponsive. I know that puts("\b \b") works so the only conclusion is that my backspace key isn't being detected as a '\b'. What can I do? Please help? Thanks in advance!
^?
perchance? – Celada Jul 30 '17 at 18:55if (c == 0x7f) ...
. – Johan Myréen Jul 30 '17 at 19:50