Java 暂停直到按下任何键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21339069/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 08:08:18  来源:igfitidea点击:

Java Pause Until Any Key Pressed

java

提问by newGuy

I was wondering if there is an option in Java which would allow you to pause program until any key pressed. This is one part of my code, eventually it is a part of a while loop in another class.

我想知道 Java 中是否有一个选项可以让您暂停程序直到按下任何键。这是我代码的一部分,最终它是另一个类中 while 循环的一部分。

public void menu()
{
    boolean exit = false;

    System.out.println("-- Command interpreter --");
    System.out.println("[1] Write results to console");
    System.out.println("[2] Write results to text file");
    System.out.println("[3] exit");
    System.out.println("-------------------------");
    System.out.print("Input Value: ");

    String s = scaner.nextLine();

    int i = Integer.parseInt(s);

    switch (i)
    {
        case 1:
        {
            writeToScr(listPopulation);
            System.out.println("\n\nPress any key to continue..");
            scaner.next();
            break;
        }
        case 2:
        {
            writeToFile(listPopulation);
            System.out.println("\n\nPress any key to continue..");
            scaner.next();
            break;
        }
        case 3:
        {
            exit = true;
            break;
        }
        default:
        {
            System.out.println("Unknown Entry.");
            break;
        }
    }

    if (exit)
    {
        System.out.println("Exit!");
        return;
    }
}

So what i want to accomplish is, for example in case 1, when i get results written to console, i want for program to pause and let me check what i have until i press any key. My code works only for ENTER, or any text + ENTER. Is there a way that i could press any key without pressing ENTER afterwards and the program continues?

所以我想要完成的是,例如在情况 1 中,当我将结果写入控制台时,我希望程序暂停并让我检查我拥有的内容,直到我按任意键。我的代码仅适用于 ENTER 或任何文本 + ENTER。有没有一种方法可以让我按任意键而不按 ENTER 并且程序继续?

采纳答案by Hovercraft Full Of Eels

The problem is with the Java console which needs to work with every operating system. This console does not allow one to listen to individual key presses. For this to work, you'll need to either create a GUI such as a Swing GUI or use an alternative 3rd party console library such as jCurses.

问题在于需要与每个操作系统一起使用的 Java 控制台。这个控制台不允许一个人听个别的按键。为此,您需要创建一个 GUI(例如 Swing GUI)或使用替代的 3rd 方控制台库(例如 jCurses)。

回答by jrowe08

I don't think this is possible through Java console. You would need some sort of API that can listen for events(key strokes(Swing or JNI for example)).

我认为这是不可能通过 Java 控制台实现的。您需要某种可以侦听事件的 API(击键(例如 Swing 或 JNI))。

回答by Kick

As you want to wait the current execution in switch case,So below is small code which can help you.It will wait till user will type click+Enter.

因为你想在 switch case 中等待当前执行,所以下面的小代码可以帮助你。它会等到用户输入 click+Enter。

Code :

代码 :

import java.lang.Thread;
import java.util.Scanner;


public class App{

    public void menu() throws InterruptedException
   {
    boolean exit = false;
    Scanner scaner = new Scanner(System.in);
    System.out.println("-- Command interpreter --");
    System.out.println("[1] Write results to console");
    System.out.println("[2] Write results to text file");
    System.out.println("[3] exit");
    System.out.println("-------------------------");
    System.out.print("Input Value: ");

    String s = scaner.nextLine();
    int i = Integer.parseInt(s);

    switch (i)
    {
        case 1:
        {
            ThreadB b = new ThreadB();
        b.start();
                synchronized(b)
                {
                    b.wait();
                }
            System.out.println("\n\nPress any key to continue..");
            scaner.next();
            break;
        }
        default:
        {
            System.out.println("Unknown Entry.");
            break;
        }
    }

    if (exit)
    {
        System.out.println("Exit!");
        return;
    }
}

  public static void main(String arg[])
    {
        try {
            new App().menu();
        } catch (Exception ex) {
           ex.printStackTrace();
        }
    }
}

class ThreadB extends Thread{

    Scanner scan = new Scanner(System.in);

    @Override
    public void run(){

        while(scan.hasNext())
        {
        String abc = scan.next();
            if(abc.equalsIgnoreCase("click"))
            {
         synchronized(this)
                {
                  notify();
        }
            }
        }
    }
}