• Advertisement
Sign in to follow this  

[java] Wait for User to Press Enter

This topic is 4276 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know that this seems really stupid, but what code is necessary for a Java program to wait until the user presses enter after some text is displayed? There just does not seem to be any working resources on this at all. Thanks in advance for any help.

Share this post


Link to post
Share on other sites
Advertisement
The class must implement KeyListener and one of the first few lines of code must be "addKeyListener(this);" (Without the quotes). You must include the void methods keyPressed(KeyEvent e), keyTyped(KeyEvent e), and keyReleased(KeyEvent e).
In the keyPressed (or keyReleased, your preference) method you must have the code
if(e.getKeyCode()==10)//10 is the keyCode for enter.
closeText();//Put what you want to do when they hit enter here.

Share this post


Link to post
Share on other sites
Just a tip:
Never use literals to represent key values (or any event, for that matter). These could change in a future version of Java. The preferred approach would be KeyEvent.VK_ENTER .

Share this post


Link to post
Share on other sites
OK, I've tried using all of the stuff you gave me and implement it into a simple Hello World demo first, but it won't compile because of an "illegal start of expression" at line 6. Here is my code:

public class KeyStrokeTest implements KeyListener{
public static void main (String[] args) {
//KeyStroke code
addKeyListener(this);

void keyPressed(KeyEvent e) {
if (e.getKeyCode()==KeyEvent.VK_ENTER) {
closeText();
}
}
void keyTyped (KeyEvent e) {}
void keyReleased (KeyEvent e) {}

System.out.println ("Hello World!");
}
}

Share this post


Link to post
Share on other sites
You are trying to nest those functions inside of the main function. I don't beleive Java supports nested functions. They should be in the class but not the main function.

public class KeyStrokeTest implements KeyListener
{
void keyPressed(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
closeText();
}
}

void keyTyped (KeyEvent e)
{
}
void keyReleased (KeyEvent e)
{
}

public static void main (String[] args)
{
//KeyStroke code
addKeyListener(this);

System.out.println ("Hello World!");
}
}

Share this post


Link to post
Share on other sites
Quote:
Original post by Simian Man
You are trying to nest those functions inside of the main function. I don't beleive Java supports nested functions. They should be in the class but not the main function.

public class KeyStrokeTest implements KeyListener
{
void keyPressed(KeyEvent e)
{
if (e.getKeyCode()==KeyEvent.VK_ENTER)
{
closeText();
}
}

void keyTyped (KeyEvent e)
{
}
void keyReleased (KeyEvent e)
{
}

public static void main (String[] args)
{
//KeyStroke code
addKeyListener(this);

System.out.println ("Hello World!");
}
}


Thanks for the help, but will I have to do anything in particular if it's in an applet as that's what it's eventually going to be placed in? Also the code you game me does not compile either.

Share this post


Link to post
Share on other sites
Hi, Is it a console app?

Because this will work just fine.

public static void main(String[] args) {
BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
System.out.println("enter somethin:\nasd");
try {
String str = r.readLine(); //will wait for a new line;
} catch (IOException e) {
e.printStackTrace();
}
}


The function readline of BufferedReader wait for a new line to continue. In our case enter would be a newline. I have not tested that on unix but I'm pretty sure it can work!

Share this post


Link to post
Share on other sites
GTspeed is correct in his code (just missing "the rest of the story" like components and windows and such. Here is a better tutorial that should help:

http://java.sun.com/docs/books/tutorial/uiswing/events/keylistener.html

Good luck.

Share this post


Link to post
Share on other sites
Well if you want to use it in an applet here is a quick helloworld applet with the additional functionality you are looking for.



import java.awt.*;
import java.awt.event.*;
import javax.swing.JApplet;

public class HelloWorldApplet extends JApplet implements KeyListener {

private boolean displayText;

public HelloWorldApplet() {
this.addKeyListener(this);
}

public void init() {
displayText = true;
}

public void paint( Graphics g ) {
if( displayText )
g.drawString("Hello World!",10,10);
else
g.clearRect(0,0,this.getWidth(),this.getHeight());
}

public void keyPressed( KeyEvent e ) {

}

public void keyReleased( KeyEvent e ) {
if( e.getKeyCode() == KeyEvent.VK_ENTER ) {
displayText = !displayText;
this.paint(this.getGraphics());
}
}

public void keyTyped( KeyEvent e ) {
//mainly used for standard editing characters
}

}






Now the source for the page that will display the applet

<html>
<head>
<title>Hello World Applet Test Page</title>
</head>
<body>
<applet
code="HelloWorldApplet"
width="400"
height="400">
If you are seeing this text you need to download
<a href="http://java.sun.com/">Java</a>
in order to properly view this web page.
Please, follow the link provided!
</applet>
</body>
</html>


I compiled and tested it and it works! Hope that helps!

Share this post


Link to post
Share on other sites
Quote:
Original post by Simian Man
You are trying to nest those functions inside of the main function. I don't beleive Java supports nested functions. They should be in the class but not the main function.

This is true, but where did this "nested function" concept come from? Does C++ support nested functions? If so, how are they used?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement