ncurses与外部shell之间的按键混乱问题

5

我有一个ncurses应用程序,它采用了临时退出ncurses的标准方法,运行外部编辑器/ shell /其他功能,然后在完成后返回到ncurses。

这 ~ 几乎可以工作,除了 ncurses 之后得到的前几个按键显然是虚假的; 如果我连续按两次向上箭头,ncurses 分别认为看到了 ^[ 和 A。

有没有人见过这种行为,并知道修复它的魔法呢?如果有帮助的话,这是 Ruby ncurses 库。

2个回答

1

在翻找一番后,我发现了一个 cargo culting 的解决方案:在从 stdscr 退出 shell 后显式调用 keypad(1)。我不知道为什么这样可行,但它确实有效。如果有人能够解释原因,我会将其标记为正确答案。目前的工作理论是 keypad 触及某种内部缓冲区并将其清除。

放弃:

NCURSES_EXPORT(int)
keypad(WINDOW *win, bool flag)
{
    T((T_CALLED("keypad(%p,%d)"), win, flag));
if (win) { win->_use_keypad = flag; returnCode(_nc_keypad(SP, flag)); } else returnCode(ERR); }

0

没错。如果没有键盘,ncurses就无法为您处理转义码。从键盘的man页面中可以看到:

   The keypad option enables the keypad of the user's  terminal.   If  en-
   abled (bf is TRUE), the user can press a function key (such as an arrow
   key) and wgetch returns a single value representing the  function  key,
   as in KEY_LEFT.  If disabled (bf is FALSE), curses does not treat func-
   tion keys specially and the program has to  interpret  the  escape  se-
   quences  itself.   If the keypad in the terminal can be turned on (made
   to transmit) and off (made to work locally),  turning  on  this  option
   causes  the terminal keypad to be turned on when wgetch is called.  The
   default value for keypad is false.

通常,在ncurses程序中,我做的第一件事就是调用keypad(stdscr, true)来启用良好的键盘映射。
希望这有所帮助。

所以,我在这里不明白的是,为什么它在两个按键后就“自动修复”了呢? - Edward Z. Yang

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接