keypress event

Using GLFW
sajjad
2014-03-30
2014-03-31
  • sajjad
    sajjad
    2014-03-30

    Hi forum,

    I am using the key events to move around the scene. Right now i have to press the key each time to move around the scene. I want to move around as long as the key is pressed down.

    Any reference or hint is requested.

    Thanks

     
  • sajjad
    sajjad
    2014-03-31

    I am not sure if i have explained my issue well enough. Lets try again.

    I want the scene camera to keep moving to right as long as the right arrow key is hold pressed. I am doing it as follows, but the the camera does not keep on moving as i hold it down. It just moves one step even if the key is pressed down.

    ////////////////////////////////////
    void key_callback(GLFWwindow *window,int key,int scancode,int action,int mods)
    {
    if(action != GLFW_PRESS)
    return;

    //return the value of the GLFW timer
    float time = glfwGetTime();

    const GLfloat degreesPerSecond = 180.0f;
    degreesRotated += time * degreesPerSecond;
    while(degreesRotated > 360.0f) degreesRotated -= 360.0f;

    const float moveSpeed = 0.02f;

    switch(key)
    {
    case GLFW_KEY_ESCAPE:
    glfwSetWindowShouldClose(window,GL_TRUE);
    break;
    //case GLFW_KEY_RIGHT:
    case GLFW_KEY_RIGHT:
    //std::cout << "Right pressed" << std::endl;
    gCamera.offsetPosition(time * moveSpeed * gCamera.right());
    break;
    case GLFW_KEY_LEFT:
    //std::cout << "Left pressed." << std::endl;
    gCamera.offsetPosition(time * moveSpeed * -gCamera.right());
    break;
    case GLFW_KEY_UP:
    std::cout << "Up pressed." << std::endl;
    break;
    case GLFW_KEY_DOWN:
    std::cout << "Down pressed" << std::endl;
    break;
    default:
    break;
    }
    }

    ////////////////////////////////////

    What did i miss in the process ?

    I am using glfw3.

    Thanks

     
    Last edit: sajjad 2014-03-31
  • Doug Binks
    Doug Binks
    2014-03-31

    You explained the problem clearly the first time :) You'll note your code is almost identical to the code listed in the post I referenced, so the solution I will suggest is the same:

    You either need to use the key callback and keep a track of the state of the keys you require, or use polling of glfwGetKey() once per key per frame. Since you're using the key callback now, I'll explain in some more detail how to do that.

    First off you need to understand how the key callback is be called. It will be called once when the key is pressed, with action == GLFW_PRESS, and once when it is released with GLFW_RELEASE. In between you may receive GLFW_REPEAT events at a rate dependent on OS and system settings, and in general you should ignore these for games.

    Because of these rules, you shouldn't use the function to do any movement processing as this would then only do movement when users press/unpress keys, instead you need to do this in your main game loop.

    You can use this as follows:

    bool moveL = false;
    bool moveR = false;
    bool moveU = false;
    bool moveD = false;
    
    void key_callback(GLFWwindow *window,int key,int scancode,int action,int mods)
    {
        bool set = (action != GLFW_RELEASE ); // true if we have GLFW_PRESS or GLFW_REPEAT
        switch(key)
        {
        case GLFW_KEY_ESCAPE:
            glfwSetWindowShouldClose(window,GL_TRUE);
        break;
            case GLFW_KEY_RIGHT:
            moveR = set;
            break;
        break;
            case GLFW_KEY_LEFT:
            moveL = set;
            break;
        break;
            case GLFW_KEY_UP:
            moveU = set;
            break
        break;
            case GLFW_KEY_DOWN:
            moveD = set;
            break;
        default:
        break;
        }
    }
    
    //mainLoop() call this every frame
    mainLoop()
    {
       //use moveL etc. to do movement code
    }