Loop break if Esc has been pressed

I wrote a program in the JAVA language that accepts input from the console using the Scanner class ....

now I want to add this ability to my code so that a while loop exists when the user presses the Esc button. so far I thought that the Keyboard class could help me, but it was like a Scanner ... I tried to use events, but I don't know how to use them correctly.

Source:

    package switchCase_v1;

     import cs1.Keyboard;
     import java.util.EventObject;
     import java.awt.AWTEvent;
     import java.awt.event.KeyEvent;
     import java.awt.event.ComponentEvent;
     import java.awt.event.InputEvent;
     import java.util.*;

      public class SwithCase {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner input = new Scanner(System.in);
        System.out.println("enter the name or number of month: ");
        int monthNumber = input.nextInt();

        while (true) {
            KeyEvent button;
            if (button.getKeyCode() == 27)
                break;
            else if (monthNumber == '\n') {
                System.out.println("enter a number");
                monthNumber = input.nextInt();
            } else {
                switch (monthNumber) {
                case 1:
                case 2:
                case 3:
                case 4:
                case 5:
                case 6:
                    System.out.println("it has 31 days");
                    monthNumber = input.nextInt();
                    break;
                case 7:
                case 8:
                case 9:
                case 10:
                case 11:
                case 12:
                    System.out.println("it has 30 days");
                    monthNumber = input.nextInt();
                    break;
                default:
                    System.out.println("it is not a valid number");
                    monthNumber = input.nextInt();
                    break;
                }
            }

        }
    }
  }

How can I deal with cases where I want to take into account the strike buttons, such as “Esc” or “Enter”? I think this should also be applicable using ASCII codes.

this is the new version of my code:

public static void main(String[] args) {
    // TODO Auto-generated method stub

    Scanner input = new Scanner(System.in);
    System.out.print("Check number of days");
    KeyEvent e;
    if (e.getKeyCode() == KeyEvent.VK_ENTER)
    {
    System.out.println("enter the name or number of month: ");
    int monthNumber=input.nextInt();
    }
    else if (Keyboard.getEventKey()==Keyboard.KEY_ESCAPE)
    {
        System.out.println("GoodBye");
    }
    }   

}

but it has an error saying that the e object cannot be initialized ... !!!!! What to do?!!!

+3
2

, . , , , stdin, escape.

, GUI, AWT Swing. , , , Ctrl + C ( ).

+5

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

, :

int getKeyCode()

key code, . key code , . KeyEvent key code . , VK_A A, VK_ESCAPE Escape.

+3

All Articles