Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore backspace key from stdin

Tags:

c

stdin

backspace

I want to make a program that forces it's user to input text but doesn't allow him to erase any of it, what's a simple way of doing it in C?

The only thing I've got is (c = getchar()) != EOF && c != '\b' which doesn't work. Any ideas?

like image 661
Iceland_jack Avatar asked Jul 02 '10 17:07

Iceland_jack


People also ask

Which control character is used as a backspace in Linux?

The Linux kernel default lets Ctrl-Backspace generate BackSpace - this is sometimes useful as emergency escape, when you find you can only generate DELs. The left Alt key is sometimes called the Meta key, and by default the combinations AltL-X are bound to the symbol MetaX.

What is a backspace character?

Definitions of backspace character. a control character that indicates moving a space to the left. type of: ASCII control character, control character. ASCII characters to indicate carriage return or tab or backspace; typed by depressing a key and the control key at the same time.

How do you backspace in Unix?

we can setup the backspace key to work in the console by typing the command and press backspace key. If you have a problem using the backspace it should display you the above output. If not, it will just perform a backspace. After doing that press CTRL+c .


1 Answers

POSIX - unix version

#include <sys/types.h>        
#include <termios.h>
#include <stdio.h>
#include <string.h>

int
 main()
{

         int fd=fileno(stdin);
         struct termios oldtio,newtio;
         tcgetattr(fd,&oldtio); /* save current settings */
         memcpy(&newtio, &oldtio, sizeof(oldtio));
         newtio.c_lflag = ICANON;
         newtio.c_cc[VERASE]   = 0;     /* turn off del */
         tcflush(fd, TCIFLUSH);
         tcsetattr(fd,TCSANOW,&newtio);
         /* process user input here */

         tcsetattr(fd,TCSANOW,&oldtio);  /* restore setting */
         return 0;        
}
like image 138
jim mcnamara Avatar answered Oct 04 '22 20:10

jim mcnamara