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
Solved

Any Key problem in a Java App.

Posted on 1998-11-21
3
146 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
print map entry 34 72
ForLoop Example 3 53
arguments to jar 5 35
maven not picking latest jar instead picking old jar from .m2 12 22
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 This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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.

828 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