规范地确定光标键的转义序列 [英] Determining escape sequences for cursor keys canonically

查看:119
本文介绍了规范地确定光标键的转义序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想对光标键做出反应;为此,我想找出当前终端以规范的方式使用的转义序列。大多数解决方案在源代码中仅包含字符串 \e [A 等。我知道在目标系统使用不同的转义序列的情况下这不是可移植的。

I'd like to react on cursor keys; for this I want to find out the escape sequences the current terminal is using in a canonical way. Most solutions just have the strings "\e[A" etc. in the source code. I understand that this is not portable in case the target system uses a different escape sequence.

我尝试使用 tput 以获得正确的转义序列,但是 tput kcud1 返回的转义序列与用于向下箭头键的转义序列不同:它返回 \eOB ,但键结果为 \e [B 。但是,没有给tput赋予任何大写名称会导致此字符串;我能管理的最好的方法是tput cud,它返回 \e [%p1%dB,从中剥离所有参数,然后最终将成为所需的字符串。但是剥离参数听起来也不太好。

I tried using tput to get the proper escape sequence but tput kcud1 returns a different escape sequence than what is used for the down arrow key: It returns "\eOB" but the key results in "\e[B". But no capname given to tput results in this string; the best I can manage is tput cud which returns "\e[%p1%dB" from which stripping of all parameters then will finally be the wanted string. But that stripping of parameters also does not sound nice.

找出按箭头键时会产生正确转义序列的规范方法是什么?对于使用 tput curses 甚至是完全不同的方法的解决方案,我将感到满意。

What's the canonical way of finding out the proper escape sequence the arrow keys will produce when pressed? I would be happy about a solution using tput or curses or even a completely different approach.

推荐答案

光标键发送的内容取决于键盘模式。

What the cursor keys send depends on the keyboard mode.

tput 报告其应用程序模式值,但默认情况下,键盘处于标准模式。

tput reports their application mode value but by default, the keyboard is in standard mode.

DEKCKM 命令可用于仅更改其光标键行为,例如:

The DEKCKM command can be used to just change they cursor key behavior, example:

printf "\033[?1h"

或者您可以使用 smkx 命令将键盘设置为应用程序模式:

or you can use the smkx command which set the keyboard to application mode:

tput smkx

重置为:

printf "\033[?1l"

tput rmkx

这篇关于规范地确定光标键的转义序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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