Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent for getch() in Scala?

Tags:

c

scala

I'm looking for a function in Scala that has similar functionality to getch. While using the console, I'd like to have my program display information based on what characters the user inputs (for example, if I was displaying a text, hitting n would show the next page, and p the previous). Right now I'm using readLine or readChar, but they require the user to hit Enter after each input, a bit annoying. Plus, in Eclipse plug-in's interpreter, the input character is shown, although I guess that might be unavoidable.

like image 436
Henry Henrinson Avatar asked Sep 13 '25 04:09

Henry Henrinson


2 Answers

scala.Console.readChar, aliased in in current incarnations of Scala as Predef.getChar is the most direct way.

scala> readChar
res0: Char = !

UPDATE

This time without the pesky Enter:

scala> Console.in.read.toChar
res0: Char = !
like image 77
retronym Avatar answered Sep 14 '25 18:09

retronym


Use StdIn lib:

    StdIn.readChar()
like image 25
ebl Avatar answered Sep 14 '25 18:09

ebl