简体   繁体   English

如何使用 C 阻止退格键出现在 nCurses 中?

[英]How to stop backspace from appearing in nCurses using C?

I am currently writing an ncurses shell and in order to read input it is important to read it character by character and hence I am using the mvwgetch command.我目前正在编写一个 ncurses shell,为了读取输入,一个字符一个字符地读取它很重要,因此我正在使用mvwgetch命令。 And incrementing a counter as it reads character by character.并在逐个字符读取时递增计数器。 The problem is that whenever I press a an arrow key or a backspace their output is being printed.问题是,每当我按下箭头键或退格键时,它们的输出都会被打印出来。 So, if for example I press backspace, ^?因此,例如,如果我按退格键, ^? is being printed.正在打印。

while ((command[i] = mvwgetch(promptwin, promptline, posx)) != '\n') {
    if (command[i] == 7) { // if entered character is a backspace
        i =-2;
        posx =- 2;
        mvwdelch(promptwin, promptline, posx);
        mvwdelch(promptwin, promptline, posx - 1);
        command[i] = '\0';
    } else {
        posx++;
        posyx[1] = posx;
        wmove(promptwin, promptline, posx);
    }
    i++;
}

It is required to read characters in order to keep track of where the cursor is found on the screen.需要读取字符以跟踪光标在屏幕上的位置。 In my code, there is my attempt at solving this problem but it still is displaying these characters.在我的代码中,我尝试解决这个问题,但它仍然显示这些字符。 PS: working on linux. PS:在Linux上工作。

To start with, 7 is not backspace -- 7 is the bell. 首先,7不是空格-7是铃。 You want 8. You might also check for KEY_BACKSPACE. 您需要8。您可能还需要检查KEY_BACKSPACE。

You mention not using noecho() , but that's exactly what you have to do to suppress the output of special characters. 您提到没有使用noecho() ,但是这正是抑制特殊字符输出所要做的。 Then you can explicitly addch() the ones you want to appear (the printable characters). 然后,您可以显式addch() 显示的内容(可打印字符)。

SOLVED 解决了

Turns out the problem was that the code for backspace is 127. Therefore it was not being recognised. 原来的问题是,退格代码是127。因此未被识别。 To handle backspaces it now executes the following code. 为了处理退格,它现在执行以下代码。

if(c == 127 || c == 8){                     //if character inserted is backspace or delete
                        if(posx != tcount) {
                            mvwprintw(promptwin, promptline, (posx + 1), " ");
                            mvwprintw(promptwin, promptline, posx, " ");
                            mvwprintw(promptwin, promptline, (posx - 1), " ");
                            wmove(promptwin, promptline, (posx - 2));
                            command[(chara - 1)] = '\0';
                            chara--;
                            posx--;
                            posyx[1] = posx;
                        } else {
                            mvwprintw(promptwin, promptline, (posx + 1), " ");
                            mvwprintw(promptwin, promptline, posx, " ");
                        }
                    } else {
                        command[chara] = c;
                        posx++;
                        posyx[1] = posx;
                        wmove(promptwin, promptline, posx);
                        chara++;
                    }

This is just additional information as I cant put up comment yet.这只是附加信息,因为我还不能发表评论。 I have encountered similar issue I print the character myself discovered 8 backspace for windows terminal.我遇到了类似的问题,我打印了自己为 Windows 终端发现的 8 个退格键。 In ncurses is 7 or ^G when using noecho or ^?在 ncurses 中使用 noecho 或 ^ 时是 7 或 ^G? when using echo.使用回声时。

after char ch = getch();在字符 ch = getch(); 之后user can catch backspace as 7 and do further process themselves under noecho mode.用户可以将退格键设为 7,并在 noecho 模式下对自己进行进一步处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM