-1

I just want to know how to use the Escape Key as iput for a simple program to end a do while loop like this:

import java.util.*;
public class Escape_Key{

public static void main(String args[]){

    do{ System.out.println(" MENU ");

    }while();    //I WANT TO END THIS LOOP WHEN THE ESCAPE KEY IS PRESSED.

    }//main

  }//Escape_Key
JoseMCabrera
  • 101
  • 2
  • 4
  • 10

2 Answers2

0

Take a look to this page, it may help you:

http://docs.oracle.com/javase/tutorial/uiswing/events/keylistener.html

Specially, this part:

int getKeyCode()

Obtains the key code associated with this event. The key code identifies the particular key on the keyboard that the user pressed or released. The KeyEvent class defines many key code constants for commonly seen keys. For example, VK_A specifies the key labeled A, and VK_ESCAPE specifies the Escape key.

for more information :

break a loop if Esc was pressed

Community
  • 1
  • 1
-3

You can add boolean esc to the while test and use a thread to run inside a keylistener so it changes the boolean value once you press escape.

isherwood
  • 58,414
  • 16
  • 114
  • 157
indian
  • 130
  • 8