开发者

Read any key to continue Java

开发者 https://www.devze.com 2023-04-09 06:14 出处:网络
How can I implement the press ANY key to continue in Java. In C I use getch() and it works fine but Java I must press enter in开发者_StackOverflow中文版 every read method implement in every OutputStre

How can I implement the press ANY key to continue in Java. In C I use getch() and it works fine but Java I must press enter in开发者_StackOverflow中文版 every read method implement in every OutputStream class.

Thanks in advance.


The only way which I found was whith JNI and C.


I found a code, Equivalent function to C's “_getch()

I have given answer for this question Equivalent function to C's "_getch()" in Java? see my answer here

or use the code

public static void getCh() {
final JFrame frame = new JFrame();
synchronized (frame) {
frame.setUndecorated(true);
frame.getRootPane().setWindowDecorationStyle(JRootPane.FRAME);
frame.addKeyListener(new KeyListener() {
public void keyPressed(KeyEvent e) {
synchronized (frame) {
frame.setVisible(false);
frame.dispose();
frame.notify();
}
}

public void keyReleased(KeyEvent e) { } public void keyTyped(KeyEvent e) { } }); frame.setVisible(true); try { frame.wait(); } catch (InterruptedException e1) { } } }


You can use following code if you're using windows.

new ProcessBuilder("cmd", "/c", "pause").inheritIO().start().waitFor();

and import following package.

import java.io.IOException;

and change main function to this.

public static void main(String args[]) throws IOException, InterruptedException
0

精彩评论

暂无评论...
验证码 换一张
取 消