Readline & NCurses,处理特殊键和组合键

Readline & NCurses, handle special keys and keys combinations

我正在复制一个 Irssi interface for a student project. I would like to use NCurses for the text interface, and readline 以在编写消息时提供更好的文本编辑功能。

这个 question, answer and project 为我提供了一个很好的起点。

我的问题是我想要一个像这样的 input/event 循环:

int ch;
while (exit_condition) {
    ch = wgetch(window);
    switch (ch) {
    case ERR: continue;
    case KEY_F(1): /* do something */ break;
    case KEY_UP: /* do something else */ break;
    default: forward_to_readline(ch); break;
    }
}

但为了匹配 KEY_F(n)KEY_UP 我需要启用 keypad(window, TRUE),这将修改输入并使其无法用于 readline。

来自手册:

If keypad is TRUE, and a function key is pressed, the token for that function key is returned instead of the raw characters.

When a character that could be the beginning of a function key is received (which, on modern terminals, means an escape character), curses sets a timer. If the remainder of the sequence does not come in within the designated time, the character is passed through; otherwise, the function key value is returned. For this reason, many terminals experience a delay between the time a user presses the escape key and the escape is returned to the program.

这是我的想法:

感谢您的宝贵时间!

这是多个问题。简要地说:

  • keypad 函数的反向操作是针对给定的 window 调用该函数并将参数设置为 FALSE.
  • 一些应用程序实现了他们自己的 keypad/wgetch 版本,但是(假设您可以使用 ncurses 的 ESCDELAY 控制超时),那里没有太多收获除非你想便携,比如说,对 Solaris 诅咒。
  • 例如,使用 shift + up 不会有任何改进(这是一个依赖于终端的转义序列)
  • readline 不查看列出 shift + up 的终端数据库中的功能(它不在它知道的 termcap 的子集中)。它可以为您做的是将字符串与 readline 函数相关联。