Detecting combinations of SHIFT and CONTROL keys (solved)

Hello everyone,

I wanted to share a quick code snippet to the community. Here’s how to detect when a user holds the shift key or the control key, or both:

// Correct, works fine
bool shift_key = ((e.mods & RACK_MOD_MASK) && GLFW_MOD_SHIFT);
bool control_key = ((e.mods & RACK_MOD_MASK) && GLFW_MOD_CONTROL);

It took me a moment to figure this out. My original code looked like:

// WRONG: DOES NOT WORK
bool shift_key = ((e.mods & RACK_MOD_MASK) == GLFW_MOD_SHIFT);
bool control_key = ((e.mods & RACK_MOD_MASK) == GLFW_MOD_CONTROL);

The latter does not work in the case when both SHIFT and CONTROL are held simultaneously.

I’m just logging this in case others run across the same situation.

4 Likes