阻止 addCh() 覆盖 frollowing 字符(ncurses,c) [英] stopping addCh() from overwriting frollowing chars (ncurses, c)

查看:67
本文介绍了阻止 addCh() 覆盖 frollowing 字符(ncurses,c)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个基本的 ncurses 程序并遇到了这个问题.

I working on a basic ncurses program and ran across this issue.

我试图在不覆盖以下字符的情况下调用 addch().

I am trying to call addch() without overwritten the following characters.

基本上,我希望能够输入类似的内容

Basically, I want to be able to type out something like

elp!"

用左光标移动到第一个位置,然后在当前文本前输入h即可

Use the left cursor to move to the first position, and then type h in front of the current text to get

帮助!"

现在这种行为会导致

帮助"

用 h 覆盖第一个字符.

With the h overwriting the first character.

我的代码显示了这个问题:

My code showing this issue:

setlocale(LC_ALL, "");
initscr();
cbreak();
noecho();
intrflush(stdscr, FALSE);
keypad(stdscr, TRUE);
clear();

short currentCh;
int x, y;

while (currentCh = getch()) {
  bool modeChange = false;
  getyx(stdscr, y, x);

  if (currentCh == KEY_LEFT) {
    if (x != 0)
      move(y, x - 1);
  } else {
    addch(currentCh);
  }
}

到目前为止,我最好的想法似乎是保存在字符串中键入的所有字符,然后使用正确的新字符修改该字符串,然后每次键入字符时清除屏幕并调用 addstr()(换句话说,您正在打印字符串的内容,而不是逐个字符).

My best idea so far seems to be to save all characters typed in a string, then modify that string with the correct new characters, then clear the screen and call addstr() each time a char is typed (In other words you are printing the contents of a string, instead of character by character).

这需要您在每次 ch 后清除屏幕,并重写所有内容.

This requires you to clear the screen after each ch, and rewriting everything.

这显然很笨拙,也不理想,所以我想知道是否有更好的方法.

This is obviously janky, and not ideal, so I was wondering if there is a better way.

推荐答案

对于这种特殊情况,您可以插入而不是 addch使用 insch 的字符:

Rather than addch for that special case, you can insert a character using insch:

这些例程在下面的字符之前插入字符 ch光标.光标右侧的所有字符都移动一个空格到右边,可能是最右边的字符线路丢失.插入操作不会改变光标位置.

These routines insert the character ch before the character under the cursor. All characters to the right of the cursor are moved one space to the right, with the possibility of the rightmost character on the line being lost. The insertion operation does not change the cursor position.

例如,您可以决定在当前单元格包含空白以外的内容时使用它.您可以使用 inch 读取该字符(相似的拼写,不同的意思):

For instance, you could decide to use that when the current cell contains something other than a blank. You can read that character using inch (similar spelling, different meaning):

这些例程返回 chtype 类型的字符,在当前在命名窗口中的位置.如果为此设置了任何属性位置,它们的值被或运算到返回的值中.常数在 <curses.h> 中定义可以与 & 一起使用(逻辑与)运算符到单独提取字符或属性.

These routines return the character, of type chtype, at the current position in the named window. If any attributes are set for that position, their values are OR'ed into the value returned. Constants defined in <curses.h> can be used with the & (logical AND) operator to extract the character or attributes alone.

(如手册页所述,AND 返回值与 A_CHARTEXT 以获取 字符).

(as noted in the manual page, AND the return value with A_CHARTEXT to get just the character).

这篇关于阻止 addCh() 覆盖 frollowing 字符(ncurses,c)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆