有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Java扫描仪输入,测试是否按下按钮

Possible Duplicate:
How to read a single char from the console in Java (as the user types it)?

我正在用Java编程,我想知道它们是否是一种“等待”直到按下按钮后再继续下一部分程序的方法。我在终端运行这个。 i、 e

Scanner in = new Scanner(System.in);
.
.
.
System.out.print("Something");
/* Wait until a button has been pressed */ 
System.out.println("Something Else");

有点像在家里。next()会一直等到按下某个键,然后按enter键。 有没有一种方法可以让你等到按下任何按钮(或者点击鼠标)后再去做然后继续下一行。最好使输入不回显到屏幕上(如果可能的话)
因此,输出将是: 某物 别的

谢谢你的时间 感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

    一种方法是设置键盘/鼠标挂钩

    您可以使用Jnativehook来执行以下操作:

    JNativeHook is a library to provide global keyboard and mouse listeners for Java. This will allow you to listen for global shortcuts or mouse motion that would otherwise be impossible using pure Java. To accomplish this task, JNativeHook leverages platform dependent native code through Java's native interface to create low level system wide hooks and deliver those events to your application.

    另一种方法是编写一个KeyListener来捕获键盘输入,看看here