break a loop if Esc was pressed

2019-01-09 17:01发布

问题:

I have written a program in JAVA language which accepts inputs from console by using Scanner class....

now I want to add this ability to my code to exist a loop (while) when user presses Esc Button. so far I thought Keyboard class can help me but it was just like Scanner...I tried to use events but do not know how to use them correctly....

Source code:

    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 I can deal with cases when I want to take into consideration the hitting buttons like "Esc" or "Enter"? I think it should be also applicable by 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 e object may not have been initialized...!!!!!what should I do?!!!

回答1:

You're currently making a command-line application which reads stuff from standard input and prints stuff to standard output. How buttons presses are handled depends entirely on the terminal in which your are running your program, and most terminals won't send anything to your application's stdin when escape is pressed.

If you want to catch key events, you'll have to make a GUI application using AWT or Swing. If all you want is to terminate your program while it's running, try pressing Ctrl+C (this works in most terminals).



回答2:

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.



标签: java loops break