Termcap tgetstr 获取方向键
Termcap tgetstr getting arrow keys
我正在尝试从 termcap 数据库中获取表示箭头键的字符串。使用以下内容:
char *buffer = malloc(2048);
tgetent(buffer, getenv("TERM")); //TERM = xterm-256color
char *key_up = tgetstr("ku", &buffer); // gives me \EOA
ku
String of input sent by typing the up-arrow key.
问题是向上箭头键实际上在输入程序时作为 \E[A
传递。它也像这样传递给 cat
。我尝试了不同的终端仿真器和 shell,它们都以相同的方式通过了它。
所以我决定将这个值硬编码为键,而不是使用 ku
值,它有效,但感觉不对。
我是不是漏掉了什么?如何以编程方式获得正确的 ku
值?
终端描述是为全屏 应用程序编写的,这些应用程序使用分配给它的一个或多个终端功能进行初始化。大约一半的终端描述将终端的光标和键盘键初始化为使用应用程序模式。在应用程序模式下,这些键发送不同的字符。
ncurses FAQ My cursor keys do not work 有更详细的介绍。
如果您尝试对一些非屏幕命令行应用程序使用终端描述,您可以让您的命令解析器同时处理 \E[
(CSI ) 和 \EO
(SS3) 一样,忽略两种模式的区别。如 xterm manual page.
zsh
的某些配置中完成的
顺便说一下,如果您的 "termcap" 实际上是一个 terminfo 系统(例如 ncurses)的接口,则不需要分配缓冲区,因为它会被忽略。 ncurses' manual 说:
- The emulation ignores the buffer pointer
bp
. The termcap library would store a copy of the terminal description in the area referenced by this pointer. However, ncurses stores its terminal descriptions in compiled binary form, which is not the same thing.