LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: EinDounat on November 23, 2014, 11:37:13

Title: How to use glfwSetKeyCallback() in LWJGL
Post by: EinDounat on November 23, 2014, 11:37:13
Hello!
I am using LWJGL 3 since yesterday and I have a question: How can I handling keyboard input with glfwSetKeyCallback(). I studied the GLFW documentation about Input handling and written this:

Main.java

...
public static void init() {
...
glfwSetKeyCallback(window, Keyboard.eventUpdate);
...
}

public static void loop() {
...
while (glfwWindowShouldClose(window) != GL_TRUE) {
...
glfwPollEvents();
...
}
}
...


Keyboard.java

...
public static void eventUpdate(long window, int key, int scancode, int action, int mods) {
if (key == GLFW_KEY_W && action == GLFW_PRESS) {
TestField.moveInDirection("UP");
} else if (...) {
...
}
}
...


I followed this guide http://www.glfw.org/docs/3.1/input.html, but I can't run the code, because glfwSetKeyCallback() needs in LWJGL glfwSetKeyCallback(long window, long cbfun), but I gave it a window (long) and a function (not long) like in the documentation.

How can I handle keybord input?

Thanks in advance!
~ EinDounat
Title: Re: How to use glfwSetKeyCallback() in LWJGL
Post by: SHC on November 23, 2014, 12:02:38
You have to use a WindowCallbackAdapter like this.


WindowCallback.set(window, new WindowCallbackAdapter()
{
    @Override
    public void key(long window, int key, int scanCode, int action, int mods)
    {
        // Do whatever you want here.
    }
});

Here's a list of other methods which you can override in the callback adapter.

(http://i.imgur.com/zBX93Ya.png)

Hope this helps.
Title: Re: How to use glfwSetKeyCallback() in LWJGL
Post by: EinDounat on November 23, 2014, 12:14:59
Oh, thanks for your fast answer! This really helped me.