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.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
joel
  • 243
  • 1
  • 2
  • 4

5 Answers5

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

Community
  • 1
  • 1
SiegeX
  • 135,741
  • 24
  • 144
  • 154
  • 3
    +1. See also: http://stackoverflow.com/questions/3167733/ignore-backspace-key-from-stdin – MrGomez Dec 06 '10 at 04: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. – David Gelhar Dec 06 '10 at 05: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 06 '10 at 05:35
  • How do you get the shadowed box to represent a key with Markdown? – Karl Knechtel Dec 06 '10 at 05:38
  • @SiegeX It turns out that in my terminal application, the ASCII code for "backspace" is 8 and not 127 as noted by Mr. Gelhar. – joel Dec 06 '10 at 05:58
  • 1
    @Karl Knechtel Key Text – SiegeX Dec 06 '10 at 06:17
  • @David Gelhar updated to reflect your comment. Thank you for that tip. – SiegeX Dec 06 '10 at 06:17
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.

Adrian Lopez
  • 1,695
  • 1
  • 16
  • 22
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.

user531771
  • 11
  • 1
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?

Douglas G. Allen
  • 2,203
  • 21
  • 20
-4

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

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124