Solved

Any Key problem in a Java App.

Posted on 1998-11-21
7
203 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now