0

I'm doing my own input window with slick, user will need to type something into this. But when I type any letter I get more than one. I assume it depends on CPU, my current bypass is to sleep thread for 100 ms whenever I type. Is there any other way to do this and get only one letter ?

This is my code

    if (input.isKeyDown(Input.KEY_0)) {
        IPInput += "0";
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
Jack
  • 16,506
  • 19
  • 100
  • 167
ashur
  • 4,177
  • 14
  • 53
  • 85

2 Answers2

1

Based off of my comment....

You can substitute isKeyPressed() in for isKeyDown().

KeyPress - triggered when the user presses a key and releases it (key down and then key up)

KeyDown - triggered when the user presses the key (key down)

Max
  • 5,799
  • 3
  • 20
  • 27
0

You can always use keyboard listeners, who has better control over what you are using your keyboard for.

In one of your BasicGameStates you can use:

public class Game extends BasicGameState{

public String answer = "";


    public void keyPressed(int key, char c) {

            if (key == Input.KEY_0) {
                answer += "0";
            }

                super.keyPressed(key, c);
    }

    public void keyReleased(int key, char c) {

            if (key == Input.KEY_0) {
                answer += "0";
            }

                super.keyReleased(key, c);
    }
}

The keyPressed response is when the key gets PRESSED rather than the other which reacts when the key is RELEASED.