Determining escape sequences for cursor keys canon

2019-02-27 17:47发布

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.

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.

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.

2条回答
ゆ 、 Hurt°
2楼-- · 2019-02-27 18:34

What the cursor keys send depends on the keyboard mode.

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

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

printf "\033[?1h"

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

tput smkx

Reset with:

printf "\033[?1l"

or

tput rmkx
查看更多
Deceive 欺骗
3楼-- · 2019-02-27 18:49

I think what you needed is tput cud1 (without the k). You can also get a list of tput commands here:

http://www.tldp.org/HOWTO/Bash-Prompt-HOWTO/x405.html

Also you can store those sequences on a variable through VAR=$(tput xyz).

And everything is in man terminfo. Online: http://www.manpagez.com/man/5/terminfo/

查看更多
登录 后发表回答