-1
\$\begingroup\$

How to make a method in LWJGL 3 that when you press a key it tells you when its pressed and when its released only one time.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ This question does not show research effort at the moment. Doing a quick search for "LWJGL3 press" yields many promising leads on the first page. Please edit your question, adding details about what you understand so far from following these guides and tutorials, and focusing on what specific issue you're having trying to implement this feature. \$\endgroup\$
    – DMGregory
    Commented May 27, 2017 at 0:50

1 Answer 1

0
\$\begingroup\$

Handling input in LWJGL3 is easily done by using GLFW. In LWJGL2 they used their own bindings for keys. I still don't know why they switched. In LWJGL3 we need to call glfwPollEvents() to get the events that have already been received.

Another thing you might want to use is glfwSetInputMode(window, mode, value). The mode must be one of GLFW_CURSOR, GLFW_STICKY_KEYS or GLFW_STICKY_MOUSE_BUTTONS. For GLFW_STICKY_KEYS and GLFW_STICKY_MOUSE_BUTTONS the value can be one of GLFW_TRUE and GLFW_FALSE. With GLFW_CURSOR the value can be one of GLFW_CURSOR_NORMAL, GLFW_CURSOR_HIDDEN or GLFW_CURSOR_DISABLED.

Now you are asking: How to make a method in LWJGL 3 that when you press a key it tells you when its pressed and when its released only one time.

So from this I assume you want to log the keys, and if any key is pressed/released you print that log. LWJGL provides a GLFWKeyCallback class for this. But you need a strong reference to the callback, so that it won't get garbage collected, so just put a reference like private GLFWKeyCallback keyCallback in your class.

glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {

    @Override
    public void invoke(long window, int key, int scancode, int action, int mods) {
        /* Do something */
    }
}

This now polls for input from the keyboard. If you wanted to print what key it was you could say

System.out.prinltn("KEY: " + key + " was pressed");

Inside the invoke method.

Lets say you wanted to do only specific keys:

first, create a field for the key like this

int ctrlAlt = GLFW_MOD_ALT | GLFW_MOD_CONTROL;

Then if you want to check if it was pressed and only log that, you could say:

if ((mods & ctrlAlt) == ctrlAlt && key == GLFW_KEY_F && action == GLFW_PRESS) {
    System.out.println("Control + Alt + F was pressed!");
}

This can be applied to mouse and text input as well.

reference: https://github.com/SilverTiger/lwjgl3-tutorial/wiki/Input

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .