java - GLFW with LWJGL - glfwGetKey once -
i'm developing game java , lwjgl3. don't understand how fix problem, i've tried search on internet information lwjgl minimal , couldn't find glfw.
let's example have menu 4 buttons (new game, options, credits, exit) , count variable know 1 selected. every time arrow key pressed want subtract 1 select, select previous one, , same down arrow key, want add one.
the problem following: if in frame 0 press arrow key count variable added in next frame key still pressed, added again. don't know how fix this, tried changing glfw_press glfw_release, want action happen when key pressed.
if ((glfwgetkey(window, glfw_key_up) == glfw_repeat) && select > 0) { button[select].toggleselect(); select--; button[select].toggleselect(); } if ((glfwgetkey(window, glfw_key_down) == glfw_repeat) && select < button.length) { button[select].toggleselect(); select++; button[select].toggleselect(); }
i know code can throw array index out of bounds exception
you should register key callback:
private glfwkeycallback keycallback; // ... keycallback = new glfwkeycallback() { @override public void invoke(long window, int key, int scancode, int action, int mods) { if (action == glfw_press) { if (key == glfw_key_up && /*...*/) { // ... } if (key == glfw_key_down && /*...*/) { // ... } } } } glfwsetkeycallback(window, keycallback);
the invoke()
method called every time key event generated. action
parameter can take 3 values:
glfw_press
when key pressedglfw_repeat
afterwards, when key still being pressedglfw_release
when key wast released
while glfwgetkey()
returns glfw_press
or glfw_release
, key callback uses glfw_press
first time key being pressed , glfw_repeat
afterwards, allowing differentiate between first frame during key pressed , subsequent ones.
Comments
Post a Comment