+1 vote
by (130 points)

Hi,

I'm using the SshTerminal Control and i need to send the F4 key but the SshTerminal does not accept the commands that i send, i tried with the follow commands

scripting.Send(FunctionKey.F4);
scripting.Send(FunctionKey.F4, ConsoleModifiers.Shift);
scripting.Send(FunctionKey.F4, ConsoleModifiers.Control);
scripting.Send(FunctionKey.F4, ConsoleModifiers.Alt);
scripting.Send(ConsoleKey.F4, ConsoleModifiers.Alt);
scripting.SendCommand("^[OS");
scripting.Send("^[OS");

1 Answer

0 votes
by (15.2k points)

Hi,

F4 key needs to be translated to an character sequence which is commonly called escape sequence. This name came from a fact that these sequences starts with actual ESC character, 0x1B in hexa (that is not '^' character).

As you wrote, sending F4 function key is done with one of the following lines, depending whether you want to combine it with CTRL, SHIFT or ALT:

scripting.Send(FunctionKey.F4);
scripting.Send(FunctionKey.F4, ConsoleModifiers.Shift);
scripting.Send(FunctionKey.F4, ConsoleModifiers.Control);
scripting.Send(FunctionKey.F4, ConsoleModifiers.Alt);
scripting.Send(ConsoleKey.F4, ConsoleModifiers.Alt);

When you use one of these, you can instruct our terminal how to translate the F4 key to an escape sequence, which will be sent to the server. There is Terminal.Options.FunctionKeysMode property which makes it possible to change those escape sequences according with your server.

Scripting.SendCommand method is usually used to send actual command, like "ls -l" or "uname" and it automatically reads the response from the server until the command followed by "new line" character is received. It is so that the server is responding with the command itself first to make it possible to read the command for the user. You need to keep in mind that the terminal is originally designed for the human users and not for the computer agents.
Scripting.Send method on the other hand sends what it gets and do not read the response at all. You can use this method if you know exactly, what F4 function key escape sequence is expected on your server.

It doesn't matter which scriting.Send() overload you use, you have to "read" the response afterwards using one of the following methods:

scripting.WaitFor(...)
scripting.ReadUntil(...)
scripting.Process(...)
...