Solved

Any Key problem in a Java App.

Posted on 1998-11-21
7
207 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
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 150 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Suggested Courses

752 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