3

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.

Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69

3 Answers3

0

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) { } } }
Community
  • 1
  • 1
Nagaraju Badaeni
  • 890
  • 8
  • 14
0

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
Maximouse
  • 4,170
  • 1
  • 14
  • 28
0

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

Jordan Borisov
  • 1,603
  • 6
  • 34
  • 69