0

In java, the keypress in console requires an "enter" to register the entry, at least with Scanner userInput. I tried simulating the enter keypress to overpass this but its dumb, at the end i need to see if "G" Was Keypressed. So im looking for a shorty understandable code to see if "G" was pressed to start the event. This is the code i have right now but it requires an enter press.

    public void WaitGetImput()
    {
        start();
        Scanner userInput = new Scanner(System.in);

        while(!userInput.hasNext());

        String input = "";
        if (userInput.hasNext()) input = userInput.nextLine();

        if (input.equals("g")) 
        {
            stop();
            System.out.println("Took "+getSeconds() +" Seconds.");
        }
    }

Start and stop are functions to start and stop a chronometer to see how much time it took. Thats why i need to bypass the enter, the enter takes time and gives a wrong read of the real reaction time. If anybody has an idea, please include he libraries to add. Im kind of a begginner in java but i've coded much in all kind of C and in python. Until now i didnt find anything that worked and was understandable at the same time, help!

Edit: Gonna learn libGDX to make it work so this topic is closed. Thanks for the help!

  • 2
    Possible duplicate of [How to read a single char from the console in Java (as the user types it)?](https://stackoverflow.com/questions/1066318/how-to-read-a-single-char-from-the-console-in-java-as-the-user-types-it) – isaace Sep 07 '17 at 13:27
  • @isaace Yeah, could work. Im on windows, i'll look up how this works. Do you think putting and interface makes the thing easier instead of needing raw windows input? – Gabriel Le Courtois Sep 07 '17 at 13:42
  • You definitely have more options when using a GUI like swing or something else. – isaace Sep 07 '17 at 13:47

0 Answers0