避免Java标准输入流中的控制序列(如^ [[C)) [英] Avoid control sequences(like ^[[C) from Java standard inputstream

查看:30
本文介绍了避免Java标准输入流中的控制序列(如^ [[C))的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

  import java.util.Scanner;公开课尝试{公共静态void main(String args []){扫描仪sc =新的扫描仪(System.in);System.out.println(输入字符串:");字符串s = sc.nextLine();System.out.println(输入的字符串是:"+ s");System.out.println(输入字符串的长度是:" + s.length());sc.close();}} 

输出:

 ┌─[jaysmito @ parrot]─[〜/Desktop]java──╼$ java试试输入一个字符串:你好输入的字符串是:hello输入字符串的长度为:5┌─[jaysmito @ parrot]─[〜/Desktop]java──╼$ java试试输入一个字符串:你好^ [[C输入的字符串是:hello输入字符串的长度为:8 

当我按下箭头键^ [[C时出现,而不是光标移动(其他箭头键,退出键,home,end也会发生类似的情况!)!

这是字符串第二次包含以下字符的情况:

  ['h','e','l','l','o','\ x1b','[','C'] 

因此,'\ x1b','[','C'是从键盘发送到外壳的字符序列,用于表示向右箭头键(光标向前).

我想要的是这些字符不会显示在外壳中,但是光标会移动(按所按下的键向前,向后,到原点,结束等).

输入后进行处理的意义不大,因为主要目的是让光标移动!

我如何用Java实现呢?

唯一的目的是在使用程序时为用户提供精确的终端体验.

解决方案

所以我做了很多搜索和谷歌搜索,到目前为止,我认为不可能直接从Java进行搜索.

但是,但是,我们可以使用C ++或C来实现这一目标.

因此,最好的最佳解决方案是为此使用C ++库.

现在我对这样的库(对于Java)不了解太多,所以我编写了自己的C ++代码,并使用JNI从Java调用了它.

如果您认为每次遇到这么小的问题都太麻烦了,那么我创建了一个非常简单的Java库来完成所有这些工作.

这是GitHub页面: https://github.com/Jaysmito101/SeriousConsole

使用非常简单.

我的代码:

  import com.jaysmito.sconsole.SeriousConsole;导入java.nio.file.*;公共班级主要{公共静态void main(String [] args)引发异常{SeriousConsole.initConsole(Path.of(").toAbsolutePath().toString()+"/libseriousconsole.so");SeriousConsole.print("输入字符串:");字符串s = SeriousConsole.readLine();System.out.println(输入的字符串是:" + s);}} 

要编译:

  javac -cp SeriousConsole.jar Main.java 

要运行:

  java -cp.:SeriousConsole.jar主要 

您可以从GitHub链接下载jar和so文件.

注意:该库旨在作为 java.io.Console

的更好版本.

Code:

import java.util.Scanner;
public class Try{
  public static void main(String args[]){
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter a String : ");
    String s = sc.nextLine();
    System.out.println("The Entered String is : " + s);
    System.out.println("The Length of Entered String is : " + s.length());
    sc.close();
  }
}

Output:

┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String : 
hello
The Entered String is : hello
The Length of Entered String is : 5
┌─[jaysmito@parrot]─[~/Desktop]
└──╼ $java Try
Enter a String : 
hello^[[C
The Entered String is : hello
The Length of Entered String is : 8

When I press the arrow keys ^[[C show up instead of the cursor moving (similar thing happens with other arrow keys, escape key, home, end)!

Whats happening here is the second time the string has the characters :

['h', 'e', 'l', 'l', 'o', '\x1b', '[', 'C']

So, the '\x1b', '[', 'C' is the sequence of characters send to the shell from the keyboard for representing right arrow key(cursor forward).

What i want is that these characters will not show up in the shell but the cursor will move (forward, backward, to home, end, etc as per the key pressed).

Processing after taking the input is meaning less as the main aim is to let the cursor be moved!

How can i achieve this in Java?

[EDIT]

The one and only aim is to give the user a exact terminal like experience while using the program.

解决方案

So I did a lot of searching and googling and as of now I don't think it is possible to do so directly from Java.

But, but we can achieve this and much more with C++ or C.

So, the best and most optimal solution will be to use a C++ library for this.

Now I don't know much of any such preexisting library(for Java) so I wrote my own C++ code and called it from Java using JNI.

And if you think its too complicated to do it every time for such a small issue i have created a very simple Java Library that does all that.

Here is the GitHub Page: https://github.com/Jaysmito101/SeriousConsole

It is very simple to use it.

My code:

import com.jaysmito.sconsole.SeriousConsole;

import java.nio.file.*;
public class Main{
    public static void main(String[] args) throws Exception{
        SeriousConsole.initConsole(Path.of("").toAbsolutePath().toString() + "/libseriousconsole.so");
        SeriousConsole.print("Enter a String : ");
        String s = SeriousConsole.readLine();
        System.out.println("The String entered is: " + s);
    }
}

To compile :

javac -cp SeriousConsole.jar Main.java

To run :

java -cp .:SeriousConsole.jar Main

You can download the jar and so file form the GitHub link.

Note : This library is meant to be a better version of java.io.Console

这篇关于避免Java标准输入流中的控制序列(如^ [[C))的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆