Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Any Key problem in a Java App.

Posted on 1998-11-21
7
Medium Priority
?
210 Views
Last Modified: 2010-03-30
How can I capture a character from the user's key press without the user having to press the 'enter' key. Similar to the getch function in Turing.

This question was asked, but wasn't correctly solved.

// suggested on expert net
char ch;
ch = System.in.read();

still needs a return.

Nobody seems to know this.. and it seems so simple.
0
Comment
Question by:tin_pham
  • 4
  • 3
7 Comments
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227985
ch = InputStream.read(); reads a character from some input stream
System.in is just the standard input stream. usually it is assotiated with the keyboard, but you can redirect some other stream as standard input (for example file or the standard output of another program ...)

the problem when using keyboard as standard input device is that you can change the text that you are entering (using backspace / arrows) before you press enter. So you confirm the text you typed only after you press Enter and that's the moment when System.in.read(); can give you the valid next character

(Note: system.in.read is NOT keyboard - its just a standard input stream. you have problems because keyboard as input device sends the text just after it has been validated (the user has pressed Enter key))

if you need to capture a user's key press on some visual component - you have to use
component.addKeyListener(mykeyListener)
and your listener will be informed about every key press (this IS keyboard now :)

hope this helps
  heyhey

(you can ask at Customer Services to delete your other question ...)
0
 

Author Comment

by:tin_pham
ID: 1227986
Thanks for the explanation. It was really helpful I wish there was a way I could give you points and yet leave up this question as unanswered.

But that still doesn't solve my problem of capturing the key event because I have to do this in an application. Maybe I have to write my own input stream.
BTW What other question?
0
 
LVL 16

Expert Comment

by:heyhey_
ID: 1227987
if you need to build a console application you don't have a "kewyboard concept" in Java, so you can't "capture key event". in console applications you have a stream of characters comming in and another stream going out ...
if you're going to build a GUI application you can "capture key event" on every Component using keyListeners ...
so ?
isn't that an answer ?

  heyhey

P.S. (you've acidently posted two identical questions :) - and you can ask costomer services to  delete the other one

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:tin_pham
ID: 1227988
You are right, now I understand what you're saying. Now how do I give you the points? The points thing is gone.
0
 
LVL 16

Accepted Solution

by:
heyhey_ earned 600 total points
ID: 1227989
hi tin_pham,
i am posting a dummy answer (greedy to get the points :-)

do you have some other problems ?

best regards
  heyhey
0
 

Author Comment

by:tin_pham
ID: 1227990
Adjusted points to 150
0
 

Author Comment

by:tin_pham
ID: 1227991
Hope you got the points.

Thanks, I'm pretty much done any Java apps for now. Next on the list for me is Java Script.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question