Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Any Key problem in a Java App.

Posted on 1998-11-21
7
Medium Priority
?
209 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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 …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

705 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