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.

4
  • have you tried looking at 'keypad' function? Dec 6, 2010 at 4:56
  • 7
    @Andrew White: Bell is \a.
    – ephemient
    Dec 6, 2010 at 4:58
  • 2
    and also you need two equal sign == instead of = for comparison Dec 6, 2010 at 5:01
  • 1
    @ephemient I stand corrected, thanks for clearing that up. Dec 6, 2010 at 5:04

5 Answers 5

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 ==

7
  • 3
    +1. See also: stackoverflow.com/questions/3167733/…
    – MrGomez
    Dec 6, 2010 at 4:58
  • 5
    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. Dec 6, 2010 at 5:33
  • @SiegeX Thanks for your answer. It was very helpful. Also, I stand corrected with my use of the assignment operator rather the equality operator. Cheers.
    – joel
    Dec 6, 2010 at 5:35
  • How do you get the shadowed box to represent a key with Markdown? Dec 6, 2010 at 5:38
  • 1
    @Karl Knechtel <kbd>Key Text</kbd>
    – SiegeX
    Dec 6, 2010 at 6:17
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.

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.

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?

-4

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

1
  • It is not "the system input driver"; it is another program that is formatting the input to your program. Dec 6, 2010 at 5:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.