0

I am writing the input for a Java game using LWJGL3 but I have an issue with input, when I press a key it has to be held for a fraction of a second before it is picked up. How do I make it see the input as soon as it is pressed?

public class InputManager extends GLFWKeyCallback {

    private long window;
    public static boolean[] keys = new boolean[65536];

    public InputManager(long window){
        this.window = window;
    }

    public void setWindow(long window){
        window = window;
    }

    @Override
    public void invoke(long window, int key, int scancode, int action, int mods) {
        keys[key] = action != GLFW_RELEASE;
    }
}

public class DisplayManager {
    private static GLFWErrorCallback errorCallback;
    private static GLFWKeyCallback keyCallback;

    public static void createDisplay() {
        inputManager = new InputManager(window);
        GLFW.glfwSetKeyCallback(window, keyCallback = inputManager);
    }

    public static void updateDisplay() {
    GLFW.glfwPollEvents();

    if (inputManager.keys[GLFW_KEY_SPACE]){
        System.out.println("Spacebar pressed.");
    }
    }
}
Althaen
  • 47
  • 10

1 Answers1

0

You are reading the keys ONLY in update display. Update display is being called indeed only few times within a second. You can see in the API how you override the triggered event from another method and store the key to access it later in updatedisplay().

  • Dear @Althaen there are people here who downgraded their teacher's salaries and tried to convince them their teaching is bad. Since the one who answered you is banned as a result of many downvoted answers by such Wonderful and Respectful One kind of a Men (I apologies but the policies of SOF won't allow to tell you what I think about them....). Hence I'll expand on that by myself: Google 'LWJGL3 read key' you will find a requirement to make callback. Callback is called right away, no need to wait for it. Reference: https://github.com/SilverTiger/lwjgl3-tutorial/wiki/Input – Vitali Pom Jan 06 '19 at 19:48