How is Esc,Alt,Ctrl and arrow keys encoded in ssh/shell

匆匆过客 提交于 2020-01-13 06:59:08

问题


I am making a SSH client for a special device which does not have a all keys on it's keyboard, my question is how is the Esc,Alt,Ctrl and arrow keys encoded in the string sent to the shell? is it just '\033'?

I know how the Enter key behaves, it gives an ^M, from here

But when i press Ctrl+v and then Ctrl nothing appears, when i press Ctrl+v and then Ctrl+c in the teminal i get: ^C , so is Ctrl just ^ ?

But what about alt

Further more i found:

left ^[[D right ^[[C
up ^[[A down ^[[B

can i just write these commands as command below, to libssh:

rc = libssh2_channel_write(self.channel, [command UTF8String], strlen([command UTF8String])))

The problem is I get the following response from ssh: zsh: substitution failed in both bash on my mac and in my SSH program:

-bash: :s^[^C: substitution failed


回答1:


Key sequence encoding depends on the terminal device. Each terminal device can make up its own encoding. The Linux console, xterm, GNU screen, gnome-terminal, konsole, vt220, etc. all have different (but similar) capabilities and key encodings.

If you need to know the encoding of a key sequence for a particular terminal device, then you should query the terminfo database for the TERM you are interested in.

Chances are the terminal emulates something similar to ECMA-48, so that it s a starting point. However, do not hard-code ECMA-48 into your application; some users might use a different terminal—especially if they ssh into the system before using your application to ssh out.

See the ncurses web site for more documentation.

If you are attempting to create your own terminal device (e.g., emulating a user pressing keys at a terminal), then it's up to you to choose the encoding you want. For it to work, you'll have to make sure your terminal device's key encoding is in the remote system's terminfo database and that the TERM environment variable is set properly. To minimize the work you need to do, you're probably best off emulating an existing popular terminal, such as GNU screen or vt220. To get their key encodings, see their terminfo database entries.



来源:https://stackoverflow.com/questions/17431836/how-is-esc-alt-ctrl-and-arrow-keys-encoded-in-ssh-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!