0

我有一个设置为 ~(ICANON) 模式的终端,我想知道如何将我获得的数据用于退格(这是 ^?)所以我可以发送一个 putchar('\b') 到控制台去退一格。

编辑:

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.
}

谢谢

4

1 回答 1

1

终端处于原始模式 ( ~ICANON) 时,BkSp密钥将输出 byte 0x7f,终端不会将其解释为退格键。(这样可以将它与按键区分开来^H。)如果您希望终端将此按键解释为退格键,您将需要:

  1. 禁用终端 ( ~ECHO) 上的回显,然后
  2. 回显输入的大多数字符,但回显0x7f0x08( \b)。(您可能还需要回显\n\r\n。)
于 2014-03-22T06:51:17.860 回答