GLFW KeyCallback Only certain keys seem to work

Started by Lugen, July 06, 2015, 08:06:27

Previous topic - Next topic

spasi

The nightly build has a temporary fix for this. A better fix is coming to GLFW soon.

Lugen

Quote from: spasi on July 31, 2015, 21:11:56
The nightly build has a temporary fix for this. A better fix is coming to GLFW soon.
Thanks for the info.

I may have another problem, unless it's just me not knowing what I'm doing.
So as before I've set up the GLFWKeyCallback method like this.

public class Keyboard extends GLFWKeyCallback {

	private static boolean[] down = new boolean[GLFW.GLFW_KEY_LAST + 1];
	private static boolean[] pressed = new boolean[GLFW.GLFW_KEY_LAST + 1];
	private static boolean[] released = new boolean[GLFW.GLFW_KEY_LAST + 1];

	public void invoke(long window, int key, int scancode, int action, int mods) {
		System.out.println("PRESSED " + key);
		down[key] = action != GLFW.GLFW_RELEASE;
		pressed[key] = action == GLFW.GLFW_PRESS;
		released[key] = action == GLFW.GLFW_RELEASE;
	}
}


I call glfwPollEvents() every update which runs at 60 FPS but judging from the print to console it seems like invoke is only called about 20 times per second if I hold down a button constantly. Is there a way to control this or perhaps some good alternative way I can set up keyboard control without using the callback?
I'm trying to track when a button is pressed for the first time. Am I wrong to assume that is what "GLFW_PRESS" does, cause it seems to be doing that but pressed[key] doesn't get set to false until a second has passed.

spasi

The behavior you're seeing is normal and depends on the OS and the keyboard hardware/driver. It will become clear if you just print the callback events, instead of printing the current state of the down/pressed/released arrays:

- You first get a GLFW_PRESS event for the key.
- After some time (several hundred milliseconds at least), the OS starts sending repeat events and you get your first GLFW_REPEAT event.
- The GLFW_REPEAT events have lower frequency than your event loop (the number of times you call glfwPollEvents()).

Quote from: Lugen on August 03, 2015, 18:43:32I call glfwPollEvents() every update which runs at 60 FPS but judging from the print to console it seems like invoke is only called about 20 times per second if I hold down a button constantly. Is there a way to control this or perhaps some good alternative way I can set up keyboard control without using the callback?

Even if you could, it's bad practice. You shouldn't mess with the user's keyboard repeat rate, it's a personal preference.

You could implement your own event repeating though; start a timer/counter on GLFW_PRESS and fire an event every x frames/milliseconds, where x is the repeat rate you want. Stop on GLFW_RELEASE.

Quote from: Lugen on August 03, 2015, 18:43:32I'm trying to track when a button is pressed for the first time. Am I wrong to assume that is what "GLFW_PRESS" does, cause it seems to be doing that but pressed[key] doesn't get set to false until a second has passed.

Correct, GLFW_PRESS is fired only once, the moment the key is pressed. As I explained above, the behavior you're seeing is caused by the code you used, simply because the first GLFW_REPEAT event is delayed.