Pascal Arsenal
Pascal Arsenal

Reputation: 52

How can I use the glfwgetkeycallback function inside of a class?

glfwGetKey always returns true while the key is pressed instead of just once.

This does not happen in the callback function, but how can I implement it inside of a Input class, so that I can modify a field in that class?

class Input {
    GLFWwindow* window;
    float up;
public:
    Input(GLFWwindow * window) {
        this->window = window;
    }

    static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
    {
        if (key == GLFW_KEY_W && action == GLFW_PRESS) {
            up = true;
        }
        else
        {
            up = false;
        }
    }

    float vertical() {  
        glfwSetKeyCallback(window, key_callback);
        return up;
    }
};

Upvotes: 1

Views: 109

Answers (1)

The Techel
The Techel

Reputation: 873

It's common among C-libraries to associate an user defined pointer with an object. Use glfwSetWindowUserPointer to set user data on the window, in this case the this pointer, and retrieve it with glfwGetWindowUserPointer. Have a look at the reference: https://www.glfw.org/docs/3.3/group__window.html#ga3d2fc6026e690ab31a13f78bc9fd3651

Upvotes: 2

Related Questions