从用户读取输入,一次一个字符(在脚本或编译的应用程序中)?

时间:2011-12-20 08:37:55

标签: scala console console-application

this question中,建议使用Console.in.read.toChar。它在REPL中工作正常,但是当我把它放入脚本时需要工作(要求用户按Enter键):

#!/usr/bin/scala
!#

while (true) {
  val c = Console.in.read.toChar
  println("Got " + c)
}

所以当我输入一个字符并按Enter键时,我得到了

Got a
Got

我在Ubuntu 11.04上使用Scala 2.9.0.1,在gnome-terminal中。

我做错了什么?

2 个答案:

答案 0 :(得分:3)

查看类似问题的答案here

问题是Java中的控制台因此Scala处于缓冲模式,需要处于原始模式才能返回单个字符而不是行。

切换到原始模式是特定于平台的问题,我想,为什么它不直接受Java支持。我假设REPL以某种方式设置了原始模式。

答案 1 :(得分:-5)

从用户读取输入,一次一个字符(在脚本或编译的应用程序中)? 回复

<input type='text' id='userinfo' />
<br />
<br />
<p id="output" style="color:red; font-size:3em" >  </p>

<script type="text/javascript">
    document.getElementById("userinfo").onkeyup = function (){
        document.getElementById("output").innerHTML = this.value;
    }
</script>