首页 > 解决方案 > 使用 readChar() 后 JLine 不显示字符

问题描述

我尝试在 java 中制作简单的键盘测试器,所以我使用 Jline 读取字符而不按 ENTER,但是当我尝试从控制台读取行时,它没有显示字符。我尝试使用不同版本的 JLine(目前我使用 2.14.6),重置终端,打开 Echo 等。但仍然没有。

ConsoleReader console = new ConsoleReader();
    //ConsoleReader cr = new ConsoleReader();
    console.setKeyMap("UTF-8");
    
    System.out.println("Press ESC for end testing");
    int key = 0;
    while(key != 27) 
    {
        key = console.readCharacter(true);
        System.out.println(key + " : " + Character.getName(key));
    }
    
    
    
    //console.getTerminal().setEchoEnabled(true);
    console.getTerminal().init();
    console.getTerminal().reset(); 
    
    console.close();
    console.delete();
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Enter text: ");
    
    System.out.println(br.readLine());
    br.close();

编辑:我添加了这些行并且它有效

        console.getTerminal().setEchoEnabled(true);
        console.getTerminal().init();
        console.getTerminal().reset(); 
        console.getTerminal().restore(); 

标签: javaconsoleconsole-applicationjline

解决方案


看起来你使用的是 JLine 2 而不是 JLine 3。Jline 2 多年来一直没有维护,所以你真的应该升级到 3.x


推荐阅读