0

I have a terminal that is set in ~(ICANON) mode and wondering how I can use the data that I get for the backspace (which is ^?) so I can send a putchar('\b') to the console to go back one space.

EDIT:

struct termios new;
newt.c_lflag &= ~(ICANON);
tcsetattr(STDIN_FILENO, TCSANOW, &newt); // Set the console to send each char straight away.

char c;
while ((c = getchar()) != '\n){
   // If the backspace key is pressed, the symbols ^? appear on the screen. How do I
   // Interpret the backspace to send back a '\b' char to the screen. I don't want the ^?
   // to appear on the screen when the backspace is pressed but rather the cursor move one
   // space to the left.
}

Thanks

DangerousDave23
  • 113
  • 1
  • 2
  • 10
  • Please rephrase your question, currently it's too opaque. I can't catch what is e.g. "the data that I get for the backspace". Also I guess you are mixing input and output issues. What code is used for backspace, and how to send something to make concrete change in viewed state, is defined for a terminal in capability database (`termcap`, `terminfo`). Do you try to repeat `curses` library activity by yourself? – Netch Mar 22 '14 at 05:40

1 Answers1

2

With the terminal in raw mode (~ICANON), the BkSp key will output the byte 0x7f, which is not interpreted as a backspace by the terminal. (This is so that it can be distinguished from the keystroke ^H.) If you want this keystroke to be interpreted by the terminal as a backspace, you will need to:

  1. Disable echo on the terminal (~ECHO), then
  2. Echo back most characters as they are input, but echo 0x7f as 0x08 (\b). (You will also probably need to echo \n as \r\n.)