how to check for the "backspace" character in C
Asked Answered
B

5

24

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.

Bicapsular answered 6/12, 2010 at 4:47 Comment(4)
have you tried looking at 'keypad' function?Grackle
@Andrew White: Bell is \a.Milton
and also you need two equal sign == instead of = for comparisonLungworm
@Milton I stand corrected, thanks for clearing that up.Subjunction
H
31

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 and getch() returns 127 when you hit backspace. Therefore try:

if (key == 127 || key == 8) { ... /* Checks for both Delete or Backspace */

Also note that your sample code uses the assignment operator = when it should be using the equality operator ==

Harvard answered 6/12, 2010 at 4:57 Comment(7)
+1. See also: #3168233Telescopy
Confusingly, the actual ASCII code for "backspace" is 8 (\b, ^H); 127 (^?) is "delete". Whether the key labeled "backspace" (or "delete") on your keyboard is sending an "8" or "127" varies depending on the settings in your terminal application. You might want to check for both.Werner
@Harvard Thanks for your answer. It was very helpful. Also, I stand corrected with my use of the assignment operator rather the equality operator. Cheers.Bicapsular
How do you get the shadowed box to represent a key with Markdown?Slovenly
@Harvard It turns out that in my terminal application, the ASCII code for "backspace" is 8 and not 127 as noted by Mr. Gelhar.Bicapsular
@Karl Knechtel <kbd>Key Text</kbd>Harvard
@David Gelhar updated to reflect your comment. Thank you for that tip.Harvard
Y
1

The type of i/o stream may helps. Standard input stream is a kind of line buffered stream, which do not flush until you write a '\n' char into it. Full buffered stream never flush until the buffer is full. If you write a backspace in full buff stream, the '\b' may be captured.

Reference the unix environment advantage program.

Yammer answered 6/12, 2010 at 5:14 Comment(0)
B
1

You didn't say which library the getch() function comes from (it isn't part of the C standard), but if it's the one from ncurses you can check the value of key against KEY_BACKSPACE.

Behead answered 7/9, 2016 at 6:57 Comment(0)
C
0

Try this:

#include <stdio.h>      /* printf   */
#include <ctype.h>      /* isalpha isdigit isspace etc      */

#define FALSE 0
#define TRUE  1

/* function declarations */
int char_type(char);

main()
{
 char ch;

 ch = 127;
 char_type(ch);

 ch = '\b';
 char_type(ch);

 return 0;
}

int char_type(char ch)
{
 if ( iscntrl(ch) != FALSE)
   printf("%c is a control character\n", ch); 
}

This is a complete program but it only tests for control characters. You could use principles of it, your choice. Just learning too!

See : http://www.tutorialspoint.com/c_standard_library/ctype_h.htm or lookup the functions for the ctype.h header file of the C Standard Library.

It's good that you're getting input. Thanks all for the info. I was just looking up backspace code and found this question.

BTW try '\0' before any char. Not sure what that does but it stops all code after it. Is that like the return 0; line?

Criminate answered 27/5, 2015 at 16:25 Comment(0)
B
-4

I believe the system input driver is line buffered. So its not possible in standard C.

Basic answered 6/12, 2010 at 4:54 Comment(1)
It is not "the system input driver"; it is another program that is formatting the input to your program.Slovenly

© 2022 - 2024 — McMap. All rights reserved.