Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading char without pressing enter

807603Nov 25 2007 — edited Nov 26 2007
Hi,

I am using the following code to read in characters from the command line:
InputStreamReader isr = new InputStreamReader(System.in);
char in = 'a';
while(true)
{
in = (char)isr.read();
//do stuff here
}
The problem with this is, the user needs to press the enter key every time they enter a character. I need the program to read the character the instant that they have pressed it, and it must not require the user to press the enter key.

I have toyed with various methods of reading from System.in, such as BufferedReaders, and reading straight from the InputStream with no luck. Could anyone give me advice on how to go about this?

Thanks,
x0psci
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 24 2007
Added on Nov 25 2007
5 comments
1,420 views