Java 用于向上、向下、向左和向右箭头的“ASCII”代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23560559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 23:55:11  来源:igfitidea点击:

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

javajquerybashascii

提问by Tk421

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

我正在编写一个从 Javascript 读取输入并将这些读数发送到 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.

我可以成功运行许多操作,例如“AZ”字母、TAB、CTRL+C 等。但我意识到我无法正确发送来敲击 ARROW UP。

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

如果我从 Javascript 中读取 ascii 代码,我会得到以下解释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/)

但是,当我将箭头向上发送到终端时,十进制键代码 38,我写了一个&符号(如遵循 ascii 表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" ?

所以,我的问题是:我必须从 Java 向 bash 发送什么代码才能告诉 bash“向上箭头”?

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

PD_ 我意识到它可能因操作系统而异,并且此代码可能不被视为 ascii 代码,因为本文建议:在此处输入链接描述

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

编辑我使用以下代码从 Java 写入 bash:

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

Thanks in advance.

提前致谢。

采纳答案by Aaron Digulla

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

“向上箭头”的转义序列是"\u001b[A". \u001bESC (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.

这意味着当你在 JavaScript 中有一个单一的关键代码时,你需要向 BASH 写入 3 个字节才能达到预期的效果。

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

您可以通过键入 来亲自查看Ctrl+VUp

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

Ctrl+V是告诉bash:“不要试图解释下一个输入,只需将其插入逐字”。

Related:

有关的: