Issues with keyboard

Using GLFW
juanito
2014-02-23
2014-03-01
  • juanito

    juanito - 2014-02-23

    Hi all,

    I wan to take inputs from the keyboard using GLFW (ver 2.7).

    Keys are read as expected, except for one issue:
    Suppose I'm moving to the left (with the left key pressed). Then, if I press the up key (keeping the left key pressed), I'm able to jump, but I stopped moving to the left.
    If I release the left key and then press it back, I'm able to continue moving to the left.

    How can I solve this issue?

    Below there is a fragment that shows the main parts of the code:

    # //global variable
    # World world;
    # 
    # //callback function
    # void readKey( int key, int action ) {
    #     world.moveGuy(key,action );
    # }
    
    # int main( int argc, char **argv ) {
    # 
    #     //...
    #     glfwEnable( GLFW_KEY_REPEAT );
    #     glfwEnable( GLFW_STICKY_KEYS );
    #
    
    #     glfwSetKeyCallback( readKey );
    #     do {
    #       //... 
    # 
    #    } while( !world.closeWindow);
    #     return error;
    # }
    
    # void World::moveGuy( int key, int action ) {
    #     if( action != GLFW_PRESS )
    #         return;
    #     if( key==GLFW_KEY_LEFT)
    #       //move left
    #    if( key==GLFW_KEY_RIGHT)
    #       //move right
    #    if( key==GLFW_KEY_UP)
    #       //jump
    #}
    

    Any help would be much appreciated!

     
  • Doug Binks

    Doug Binks - 2014-02-23

    There are two ways to do this.

    The simplest is to poll glfwGetKey() for each direction key once per frame. Alternatively, you can keep track of which keys are pressed down, and then update the movement once per frame.

     
  • juanito

    juanito - 2014-03-01

    Thanks!! I tried polling, but it didn't work.
    Using a callback function and keeping track of the stated worked@