Tk421 Tk421 - 10 months ago 52
Java Question

"ASCII" codes for arrow up, down, left and right

I am writing a program that reads input from Javascript and send those readings to bash.

I can successfully run many actions, like "A-Z" letters, TAB, CTRL+C, etc. But I realize that I cannot send properly to bash the ARROW UP.

If I read the ascii code from Javascript, I get the following as explained Binding arrow keys in JS/jQuery

37 - left
38 - up
39 - right
40 - down

However, when I send arrow up to the terminal, decimal key code 38, I write an ampersand (as by following a ascii table

So, my question is: what code do I have to send from Java to bash to tell bash "arrow key up" ?

PD_ I realize that it might be different depending on the operating system and this code might not be considered an ascii code as this post suggest: enter link description here

I write from Java to bash by using the following code:

JSch jsch = new JSch();
Channel channel = session.openChannel("shell");
OutputStream out = channel.getOutputStream();
out.write(asciiDecimalCode); // send characters

Thanks in advance.

Answer Source

The escape sequence for "Arrow up" is "\x1b[A". \x1b is the code for ESC (Escape).

That means while you have a single key code in JavaScript, you need to write 3 bytes to BASH to achieve the desired effect.

You can see it for yourself by typing Ctrl+VUp.

The Ctrl+V tells bash: "Don't try to interpret the next input; just insert it verbatim".