如何在ssh / shell中编码Esc,Alt,Ctrl和方向键 [英] How is Esc,Alt,Ctrl and arrow keys encoded in ssh/shell

查看:567
本文介绍了如何在ssh / shell中编码Esc,Alt,Ctrl和方向键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个特殊的设备制作一个SSH客户端,该设备的键盘上没有所有按键,我的问题是如何将发送到外壳中的字符串中的Esc,Alt,Ctrl和箭头键进行编码?只是'\033'吗?

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'?

我知道 Enter 键的行为如何,它从此处

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

但是当我按 Ctrl + v 然后按 Ctrl 时,什么也没有出现按 Ctrl + v ,然后在终端中按 Ctrl + c ,我得到:^ C,因此 Ctrl 只是^?

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 ^ ?

但是 alt

我进一步发现:

^ [[D
^ [[C

^ [[A
^ [[B

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

只需将这些命令作为下面的命令写到libssh:

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

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

The问题是我从ssh得到以下响应:zsh:在我的Mac和SSH程序上的bash中,替换都失败了:

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:替换失败

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


推荐答案

密钥序列编码取决于终端设备。每个终端设备可以组成自己的编码。 Linux控制台,xterm,GNU屏幕,gnome-terminal,konsole,vt220等都具有不同(但相似)的功能和键编码。

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.

如果需要知道特定终端设备的按键序列的编码,那么您应该查询 terminfo 数据库用于 TERM 您感兴趣的东西。

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.

可能是终端模仿了类似于 ECMA-48 ,因此它是一个起点。但是,请勿将ECMA-48硬编码到您的应用程序中。某些用户可能会使用其他终端,尤其是如果他们在使用应用程序进行SSH之前先进入系统。

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.

请参见 ncurses 网站以获取更多文档。

See the ncurses web site for more documentation.

如果您尝试创建自己的终端设备(例如,模拟用户在终端上按下键),然后由您来选择所需的编码。要使其正常工作,您必须确保终端设备的密钥编码位于远程系统的terminfo数据库中,并且正确设置了 TERM 环境变量。为了最大程度地减少您需要做的工作,最好是模仿一个现有的流行终端,例如GNU屏幕或vt220。要获取其键编码,请参阅其terminfo数据库条目。

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.

这篇关于如何在ssh / shell中编码Esc,Alt,Ctrl和方向键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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