In this question, it was suggested to use Console.in.read.toChar. It works fine in REPL, but fails to work when I put it into a script (requires user to press Enter):
#!/usr/bin/scala
!#
while (true) {
val c = Console.in.read.toChar
println("Got " + c)
}
So when I type a char and press Enter, I get
Got a
Got
I’m using Scala 2.9.0.1, on Ubuntu 11.04, in gnome-terminal.
What am I doing wrong?
Take a look at the answers given to a similar question here.
The issue is that the console in Java and therefore Scala is in buffered mode and needs to be in raw mode to return individual characters instead of lines.
Switching to raw mode is platform specific issue which is, I guess, why it’s not directly supported by Java. I assume that the REPL has set raw mode somehow.