I've found a lot of information about using GLUT to detect whether the Ctrl key is pressed using the GLUT_ACTIVE_CTRL macro. This macro only works, apparently, within a keyboard or mouse callback function. I need to know whether or not the Ctrl key is pressed at a point in my main loop, but GLUT_ACTIVE_CTRL doesn't seem to work in this context.
So, is there a way to detect key up and key down events on the Ctrl key (without any other keys being typed) in a platform independent GLUT-ish way?
EDIT: The keyboard callback is not fired (at least for a default setup) when the Ctrl key is pressed. This is the basic problem, that I can only test whether the Ctrl key is or isn't pressed when another key is pressed and thus fires the keyboard callback.
My setup is something like:
// ... in main function:
glutKeyboardFunc(keyboard);
//later in the code:
void keyboard(unsigned char key, int _x, int _y)
{
printf("keydown \n");
if (glutGetModifiers() == GLUT_ACTIVE_CTRL) {
printf("control key is pressed.\n");
}
//etc.
When I press any normal character "keydown " is printed to stdout. When I press the Ctrl key, nothing happens. If I press Ctrl+C, "keydown control key is pressed." is printed.
However, in my main loop I added:
if (glutGetModifiers() == GLUT_ACTIVE_CTRL) {
printf("Control key down.\n");
} else {
printf("Control key up.\n");
}
and it always prints "Control key up." regardless of whether I am pressing the Ctrl key or not.
Not using GLUT as specced. You may wish to check out GLFW.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With