Press Multiple keys


#1

I am totally beginner, I can press and catch event for a single keyboard button easily but I want to catch events for pressing multiple keys at the same time like A+B or ALT + ENTER or CTRL + A


#2

For detecting Alt or Ctrl check the mods argument to key callbacks. It is a bitmask containing info if Shift, Ctrl, Alt or Windows is pressed at the same time. Here are the symbol names: http://www.glfw.org/docs/latest/group__mods.html

For detecting other keys (will work also for Alt/Ctrl) you need to write custom code like this:

bool is_A_down = false;
bool is_B_down = false;

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
    if (key == GLFW_KEY_A) is_A_down = action == GLFW_PRESS;
    if (key == GLFW_KEY_B) is_B_down = action == GLFW_PRESS;
    if (is_A_down && is_B_down)
    {
        // now you know that both A and B keys are down, do what you need
        call_some_function();
    }
}

Of course this is very simple example. In real code you can use something like array for all the keys (512 element array) or create hashtable/std::map with your actions to call automatically. It is really your choice what your code needs to do.

Alternative is to explicitly check if keys are down:

if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS &&
    glfwGetKey(window, GLFW_KEY_B) == GLFW_PRESS)
{
    call_some_function();
}

Be careful with missed events. You might want to enable sticky keys in this case if that does not conflict with handling your other keys. Read the documentation how keyboard input works: http://www.glfw.org/docs/latest/input_guide.html#input_key