1

I'm making a strictly command prompt program and at some point i'd like to make its execution pause until the user presses a key on the keyboard (whichever would it be).

So far I tried this:

char cont =sc.next().charAt(0);
while(cont !=' ') {
  try{
    Thread.currentThread().sleep(1);
  } catch(Exception e) {
    System.out.println(e);
  }
  cont = sc.next().charAt(0);
}

In this case I wanted 'space' to resume the execution but i'd rather make any input do the trick.

EDIT: To be more specific, I'd like the program to resume instantly at any pressed key, without letting the user see what he gave as inputs.

RayureBob
  • 19
  • 1
  • 5
  • Why not simply read some input at the place where you want to wait? This will block the program until some input is there. – Henry Jan 07 '18 at 11:14
  • I should have been more specific in my question. I'd like the program not to let the user see its input in the command prompt, and instead directly resume execution at any key pressed. If I can't get to that point I'll do what you propose – RayureBob Jan 07 '18 at 11:19
  • 1. You're sleeping not enough to feel the difference - 1 millisecond. 2. You can't manipulate `System.in` to make it sleep - even the program sleeps, the input stream is still accessible. – Andrew Tobilko Jan 07 '18 at 11:30

2 Answers2

0

First of all, you don't need to do Thread.currentThread().sleep(1); for pausing the flow of the Thread. Because it pauses automatically when the program is waiting until the user introduces an input.

Instead of using a char variable, use a String one (It's easier to use with the scanner class) and remove the

try{
Thread.currentThread().sleep(1);
} catch(Exception e) {
System.out.println(e);
}

as it's not necessary. Here's the code it'd worked for me:

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    String cont = input.nextLine();
    while(!cont.equals(" ")) {

        cont = input.nextLine();

    }

}

Other solution (although much more complicated) would be to create a Frame and add to it a KeyListener (that way when the spacebar is pressed the user wouldn't see anything and the program would execute instantly).

class frame extends JFrame{

    frame(){

        addKeyListener(new KeyListener() {
            @Override
            public void keyTyped(KeyEvent e) {

            }

            @Override
            public void keyPressed(KeyEvent e) {

                if(e.getExtendedKeyCode() == KeyEvent.VK_SPACE){

                    //insert here the code you'd like to be executed when the user introduces a spacebar

                }

            }

            @Override
            public void keyReleased(KeyEvent e) {

            }
        });
      setVisible(true);
    }

}
Alex Cuadrón
  • 638
  • 12
  • 19
  • Thank you for the advices, but I edited my question and I don't think your answer would work that way. As I said, I'd like the program to detect any input, without pausing strictly speaking, and resume to what's written after instantly – RayureBob Jan 07 '18 at 11:29
  • @RayureBob check it out now. That might be what you're searching for. – Alex Cuadrón Jan 07 '18 at 11:30
  • Thank you very much this is perfect – RayureBob Jan 07 '18 at 16:41
0

In you code you don’t need any sleep calls - the input is blocking and that’s enough. But disabling the echo for input characters is tricky (not supported by Java really). Here’s a couple of workarounds:

How to disable console echoing

http://www.jguru.com/faq/view.jsp?EID=23448

algrid
  • 5,600
  • 3
  • 34
  • 37