how to read char from console

Hi can any body help me..

I want to read char from keybord. withought pressing ENTER.

I am not sure how i can do it. I can't use

BufferedReader br =new BufferedReader(new InputStreamReader ( System.in )) ;

char key ;

key = (char )br.read() ;

cuz i have to press ENTER every time to read char.

Thanks in advance.

[499 byte] By [gohan_iitda] at [2007-11-27 11:17:30]
# 1

Last I read on this, I don't think that this can be done with console. If anyone knows differently, I'd love to be wrong here. Please speak up.

petes1234a at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 2

Yea i have been using java for a year but i still dont get how to read char without pressing enter ( like a getch() on c++) on console application .

i still curious can java do a getch() function like on c++

Indra_Suryatamaa at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 3

The answer is simple: it's not possible.

prometheuzza at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 4

if your really wanted to be messy u could have a jframe hidden somewhere that captured key events but also somehow got the console to pick up the key event.. kinda like a key logger...

Futurisdom_Developera at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 5

> if your really wanted to be messy u could have a

> jframe hidden somewhere that captured key events but

> also somehow got the console to pick up the key

> event.. kinda like a key logger...

I know very little about AWT/Swing, but I'm pretty sure that (J)Frame has to have focus in order to listen for KeyEvents.

The 3rd party app JCurses can probably do what the OP is asking (not sure though):

http://sourceforge.net/projects/javacurses/

prometheuzza at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 6

Use key listeners and put another key (eg F10) to replace your enter key :)

Jamwaa at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 7

> Use key listeners and put another key (eg F10) to

> replace your enter key :)

Like I said: if your command prompt/shell has focus, no GUI component can listen for key events.

prometheuzza at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 8

> Hi can any body help me..

> I want to read char from keybord. withought

> pressing ENTER.

> am not sure how i can do it. I can't use

>

> code]BufferedReader br = new BufferedReader(new

> InputStreamReader ( System.in )) ;

> char key ;

> key = (char )br.read() ;

> [/code]

>

> cuz i have to press ENTER every time to read char.

> Thanks in advance.

Come on guys its not that we want all the heck to be listen by program. look what i want is when i ll press key for this program only and then only it should read the key.. well thanks to you all.. for devoting ur time ( which lead me to info of great knowledge!!!). I think there is not other way then using JNI. and today i am going to do that.. heck before i start hating java. i am sure they have good reason to save there ***.. that they don't have getch().

gurrrrr..... one day i ll get this one in java am sure.. but today JNI..

gohan_iitda at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 9

try this,I' have found a part on Internet

package exercises;

import java.io.BufferedReader;

import java.io.InputStreamReader;

/**

* questa la classe d' esempio per leggere l'input dalla console*/

public class Echo {

public static void main(String args[]) throws Exception{

// This is where the magic happens. We have a plain old InputStream

// and we want to read lines of text from the console.

// To read lines of text we need a BufferedReader but the BufferedReader

// only takes Readers as parameters.

// InputStreamReader adapts the API of Streams to the API of Readers;

// receives a Stream and creates a Reader, perfect for our purposes.

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

String input = "";

while(true){

System.out.print("ECHO< ");

//As easy as that. Just readline, and receive a string with

//the LF/CR stripped away.

input = in.readLine();

//Is a faster alternative to: if (input == null || input.equals(""))

//The implementation of equals method inside String checks for

// nulls before making any reference to the object.

// Also the operator instance of returns false if the left-hand operand is null

if ("".equals(input)){

break;

}else

// Here you place your command pattern code.

if ("ok".equals(input)){

System.out.println("OK command received: do something ");

}

//Output in uppercase

System.out.println("ECHO> " + input.toUpperCase());

}

System.out.println("ECHO> bye bye");

//We exit without closing the Reader, since is standard input,

// you shouldn't try to do it.

// For all other streams remember to close before exit.

}

}

Dumasa at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 10

> try this,I' have found a part on Internet

>

> ...

That was not what we were discussing.

prometheuzza at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 11

> try this,I' have found a part on Internet

Dumas.. what was that for..

i doubt that u even understand the qu? do you!!

gohan_iitda at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 12

Dumas said:

> try this,I' have found a part on Internet

The program works better if you replace the first statement in the while-loop:

System.out.print("ECHO< ");

with this:

System.out.println("ECHO< ");

(At least on my computer, the loop never stops otherwise)

prometheuzz said:

> That was not what we were discussing

The program Dumas gave us has a one-key-response feature, seems no one has come nearer the answer than this.

Ja_Lava_Javaa at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 13

> Dumas said:

> > try this,I' have found a part on Internet

>

> The program works better if you replace the first

> statement in the while-loop:

> System.out.print("ECHO< ");

with this:

> System.out.println("ECHO< ");

>

> (At least on my computer, the loop never stops

> otherwise)

Then your computer is bonkers, because that change doesn't have any effect on any loop anywhere.

> prometheuzz said:

> > That was not what we were discussing

> The program Dumas gave us has a one-key-response

> feature, seems no one has come nearer the answer than

> this.

I fail to see this feature, please enlighten us.

dwga at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 14

> The program Dumas gave us has a one-key-response

> feature, seems no one has come nearer the answer than

> this.

?

The OP was asking for a one-key-response WITHOUT pressing the return key, which is NOT what Dumas posted. The code that Dumas was already known to the OP.

The fact that "no one has come nearer the answer" is because there is no such thing possible in Java (without some native code).

prometheuzza at 2007-7-29 14:25:16 > top of Java-index,Java Essentials,Java Programming...
# 15

right, we cant do that in java withought JNI. whic is not realy a solution. its like we are creating a another big problem for solving small problem.

Message was edited by:

gohan_iitd

Message was edited by:

gohan_iitd

gohan_iitda at 2007-7-29 14:25:21 > top of Java-index,Java Essentials,Java Programming...
# 16

Answer to: dwg

> I fail to see this feature, please enlighten us.

("Feature" is maybe not the best word here - this may be a language confusion) Do not be affended. I am sure this is not a big "feature" for You dwg - but I have heard questions about this before - you know newbies also ask questions.

> Then your computer is bonkers, because that change doesn't have any effect on any loop anywhere.

The System.out.println() is terminating the line. The System.out.print() is not.

This is affecting the program here:

System.out.print("ECHO< ");

input = in.readLine();

if ("".equals(input)){

break;

}else

If you use print the input will be "ECHO< " when you press enter - if you use println input will be "" and THEN the loop will terminate.

Answer to: prometheuzz

Please do not be angry - Dumas was trying to help and I was trying to correct one line and answer a question. I think many people are here to learn (well- maybe not You because you already know everything I guess).

everyone:

(I love You all) I think that:

One friendly message - when you try to HELP, is better than

two unfriendly messages

because:

Java is Love

Ja_Lava_Javaa at 2007-7-29 14:25:21 > top of Java-index,Java Essentials,Java Programming...
# 17

> ...

> Answer to: prometheuzz

> Please do not be angry -

I am not angry.

> Dumas was trying to help and

Without a doubt that was his/her intention, but Dumas either did not read the entire thread, or s/he did not understand it because his/her answer was not correct.

> I was trying to correct one line and answer a

> question. I think many people are here to learn

Yes, that is correct (about the learning part), but people do not learn by posting or reading incorrect information. If you're not sure about something you post, please state that. By posting something you're not entirely sure of but raising the impression that you are, is misleading and to no benefit to the OP.

> (well- maybe not You because you already know

> everything I guess).

No one knows everything. Also, don't mistake a high post-count with knowledge! I'm just chatty.

; )

prometheuzza at 2007-7-29 14:25:21 > top of Java-index,Java Essentials,Java Programming...
# 18

> Answer to: dwg

> > I fail to see this feature, please enlighten us.

> ("Feature" is maybe not the best word here - this may

> be a language confusion) Do not be affended. I am

No language confusion and I'm not offended, but the feature you are talking about is not present in the code we are discussing.

> sure this is not a big "feature" for You dwg

> - but I have heard questions about this before - you

> know newbies also ask questions.

Yup, and I try to answer those I can.

>

>

> > Then your computer is bonkers, because that change

> doesn't have any effect on any loop anywhere.

>

> The System.out.println() is terminating the

> line. The System.out.print() is not.

> This is affecting the program here:

>

> System.out.print("ECHO< ");

> input = in.readLine();

> if ("".equals(input)){

> break;

> }else

>

> If you use print the input will be "ECHO< "

> when you press enter - if you use println

> input will be "" and THEN the loop will

> terminate.

NO. This is entirely false.

System.out is one thing, System.in is another. They do not pollute one another.

I've verified this by running the following codeBufferedReader input = new BufferedReader(new InputStreamReader(System.in));

System.out.print("ECHO< ");

String foo = input.readLine();

System.out.println(foo);

and pressing enter, the output as expected is nothing (i.e. "").

dwga at 2007-7-29 14:25:21 > top of Java-index,Java Essentials,Java Programming...