1
votes

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
It depends on the terminal.Richard Hansen
Libssh2 connecte to linux through a shellDavid Karlsson

1 Answers

2
votes

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.