Beakie Beakie - 2 months ago 26
C++ Question

glut - holding a key down

I have some basic code to collect key up and key down events using glut.

If I hold a key down I am getting continuous events firing (down/up/down/up/down/up/........), instead of the intended down (once, at the start) and up (once, at the end)

#include <GL/glut.h>
#include <iostream>

void keyDown (unsigned char key, int x, int y)
{
std::cout << "keydown " << key << "\n";
}

void keyUp (unsigned char key, int x, int y)
{
std::cout << "keyup " << key << "\n";
}

void render(void)
{
std::cout << "render\n";
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_SINGLE);
glutInitWindowSize(300, 300);
glutInitWindowPosition(100, 100);
glutCreateWindow("Hello world :D");
glutDisplayFunc(render);
glutKeyboardFunc(keyDown);
glutKeyboardUpFunc(keyUp);
glutMainLoop();

return 0;
}


Any input appreciated.
Thanks

Answer

Your problem is that the auto repeat key is on. To turn it off, just put, at your initialization phase this command:

glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);

Man page at: https://linux.die.net/man/3/glutsetkeyrepeat