wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes Press any key to continue... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Press any key to continue..." Watch "Press any key to continue..." New topic
Author

Press any key to continue...

D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

I know this has been discussed million times. I need read charcters from console not waiting for <cr>. System.in.read() waits for <cr>. I can't use java.io.Console yet since compatibility. Please remind me how to read keyboard.


Retire your iPod and start with HD Android music player Kamerton | Minimal J2EE container is here | Light weight full J2EE stack | and build tool | Co-author of "Windows programming in Turbo Pascal"
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

It may have been discussed many times, but the answer is always very simple: you can't do it. Furthermore, java.io.Console doesn't help: it only provides line-buffered-non-echoed reads for reading passwords, not any kind of general console-handling.

There's a library called "JLine" out there that might do this; I'm not sure. It uses native code, as any solution would need to.


[Jess in Action][AskingGoodQuestions]
D Rog
Ranch Hand

Joined: Feb 07, 2004
Posts: 472

Indeed I was under impression that java.io.Console solves the problem. It seems time to join Java net team to add the feature in 1.7.
Chris Treglio
Ranch Hand

Joined: Jun 18, 2001
Posts: 64
Could you react to intput made before a CR by subclassing FilterInputStream (with the original System.in as the underlying stream) and using your subclass to manually flush the member "in"?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Originally posted by Chris Treglio:
Could you react to intput made before a CR by subclassing FilterInputStream (with the original System.in as the underlying stream) and using your subclass to manually flush the member "in"?


No. System.in.read() won't return until Enter is pressed, no matter what.
 
 
subject: Press any key to continue...