I’d like to know how to check if a user types the “backspace” character.
I’m using the getch() function i.e. "key = getch()" in my C program and i’d like to check when backspace is pressed. the line:
if(key = '\b') { ....
doesn’t work.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The problem with reading Backspace is that most terminals are ‘cooked’ in that keys like backspace are handled by the terminal driver. However, the curses function
getch()can read the backspace as it’s not tied to the terminal.Edit
I just noticed your code is using
getch()for input. I ran a little test program andgetch()returns 127 when you hit backspace. Therefore try:Also note that your sample code uses the assignment operator
=when it should be using the equality operator==