Solved

Any Key problem in a Java App.

Posted on 1998-11-21
7
205 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 50
hibernate example using maven 12 42
ejb on wildfly 5 20
mysql jsp example issue 32 38
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…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

778 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