Read any key to continue Java

2019-05-10 08:13发布

问题:

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 every read method implement in every OutputStream class.

Thanks in advance.

回答1:

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



回答2:

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) { } } }


标签: java key