简体   繁体   English

Termcap Tgetstr获取箭头键

[英]Termcap tgetstr getting arrow keys

I'm trying to get the string representing the arrow key up from the termcap database. 我正在尝试从termcap数据库中获取表示箭头键的字符串。 Using the following: 使用以下内容:

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. ku通过键入向上箭头键发送的输入字符串。

The problem is the arrow key up is actually passed as \\E[A when typing into the program. 问题在于,向上箭头键实际上是在输入程序时作为\\E[A传递的。 It's also passed like this to cat . 这样也传递给了cat I tried with different terminal emulators and shells and they all passed it the same way. 我尝试了不同的终端仿真器和外壳,它们都以相同的方式通过了它。

So I decided to hardcode this value for the key instead of using the ku value, and it works, but doesn't feel right. 因此,我决定为该键硬编码该值,而不是使用ku值,它可以工作,但感觉不正确。

Am I missing something here? 我在这里想念什么吗? How can I programmatically get the right ku value? 如何以编程方式获得正确的ku值?

Terminal descriptions are written for full-screen applications, which are initialized using one or more terminal capabilities assigned to this. 终端说明是为全屏应用程序编写的,这些应用程序使用分配给它的一个或多个终端功能进行初始化。 About half of the terminal descriptions initialize the terminal's cursor- and keypad-keys to use application mode . 大约一半的终端描述会初始化终端的光标键和小键盘键以使用应用程序模式 In application mode, those keys send different characters. 在应用程序模式下,这些键发送不同的字符。

The ncurses FAQ My cursor keys do not work goes into more detail. ncurses常见问题解答我的光标键不起作用 ,将详细介绍。

If you are trying to use a terminal description for some non-screen command-line application, you could make your command-parser treat both \\E[ (CSI) and \\EO (SS3) as the same thing, and ignore the difference between the two modes. 如果您尝试对某些非屏幕命令行应用程序使用终端说明,则可以使命令解析器将\\E[ (CSI)和\\EO (SS3)视为同一事物,而忽略两者之间的区别两种模式。 That was done in some configuration for zsh , as mentioned in the xterm manual page . xterm手册页中所述,这是在zsh某些配置中完成的。

By the way, if your "termcap" is actually an interface to a terminfo system (such as ncurses), it is not necessary to allocate the buffer, since that is ignored. 顺便说一下,如果您的“ termcap”实际上是terminfo系统的接口(例如ncurses),则不必分配缓冲区,因为该缓冲区将被忽略。 ncurses' manual says: ncurses的手册说:

  • The emulation ignores the buffer pointer bp . 仿真将忽略缓冲区指针bp The termcap library would store a copy of the terminal description in the area referenced by this pointer. termcap库会将终端描述的副本存储在此指针引用的区域中。 However, ncurses stores its terminal descriptions in compiled binary form, which is not the same thing. 但是, ncurses以编译的二进制形式存储其终端描述,这不是同一回事。

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

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