Solved

Any Key problem in a Java App.

Posted on 1998-11-21
3
145 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 the return key to be pressed.

Nobody seems to know this.. and it seems so simple.
0
Comment
Question by:tin_pham
3 Comments
 
LVL 27

Expert Comment

by:BigRat
ID: 1227992
This is perfectly correct. It assume that you are inconsole context and here the InputStream in is buffered meaning that the Return key must be pressed.
   In which context do you want a solution? Concole context or have you some GUI on the screen?
0
 

Accepted Solution

by:
revferret earned 100 total points
ID: 1227993
You can implement the java.awt.event.KeyListener class.

This class has 3 methods:
public void keyPressed(java.awt.event.KeyEvent kevent);
public void keyTyped(java.awt.event.KeyEvent kevent);
public void keyReleased(java.awt.eventKeyEvent kevent);

This is how I do it and it seems to work.
0
 

Author Comment

by:tin_pham
ID: 1227994
Thanks to everybody who attempted to solve this. I've come to the conclusion that it's not possible because of the way Java reads the input stream.

I cannot use the AWT because this is a java Application not an Applet.


0

Featured Post

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
even odd program using while loop 3 40
HashTable highest marks enumeration alternative 9 37
Way to decrease size of apk file 9 67
servlet example 17 32
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 …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

773 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