How to use glfwSetKeyCallback() in LWJGL

Started by EinDounat, November 23, 2014, 11:37:13

Previous topic - Next topic

EinDounat

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

SHC

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.



Hope this helps.

EinDounat

Oh, thanks for your fast answer! This really helped me.