立即获取按键输入

时间:2019-01-01 23:24:31

标签: java input lwjgl

我正在使用LWJGL3编写Java游戏的输入,但是输入有问题,当我按下某个键时,必须按住它一秒钟,然后才能拿起它。如何让它在按下输入后立即看到输入?

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.");
    }
    }
}

1 个答案:

答案 0 :(得分:0)

您仅在更新显示中读取按键。实际上,在一秒钟内仅几次调用更新显示。您可以在API中看到如何从另一个方法重写触发的事件,并存储密钥以供以后在updatedisplay()中访问它。