5

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 http://www.asciitable.com/)

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

Edit 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.

Community
  • 1
  • 1
Tk421
  • 6,196
  • 6
  • 38
  • 47
  • 6
    You mention Java, Javascript and bash here. Whatever are you doing? –  May 09 '14 at 09:04
  • Why are you trying to send up arrow to bash? A java program is not a user. – Peter Lawrey May 09 '14 at 09:05
  • 1
    Reading the characters from JavaScript and sending it to bash using Java. In the context makes sense. – Tk421 May 09 '14 at 09:05
  • 1
    Those codes you have are not ASCII codes, they are just key mapping codes of the keyboard, and may vary depending on manufacturer and localization. – Teemu May 09 '14 at 09:08
  • That means that it cannot be done ? – Tk421 May 09 '14 at 09:10
  • he seems to be trying to make a web terminal , it can be used to get all info about your server , if in case you are redirecting bash output to your page as well , and about the Question , can you show the code where your ASCII values get posted in bash. – Vivek May 09 '14 at 09:14
  • 1
    Correct, I am creating a web terminal. This program is due curiosity more than anything. It is an Javascript Terminal. (I do not understand the negative point). I edited the main question and added the code that I used to write to bash. It is just writing decimal values thought a OutputStream by using the ChannelShell of JSch. – Tk421 May 09 '14 at 09:18

1 Answers1

12

The escape sequence for "Arrow up" is "\u001b[A". \u001b 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".

Related:

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820