Tuesday, May 3, 2011

How do I tell if a button is being held down in OpenGL/Glut? (linux)

This is a similar problem: Link
Which was solved by calling GetAsyncKeyState(). While all fine and dandy, I need a Linux alternative. I need to know if a button is being held down, not just being pressed (because of the keyboard buffer delay).

Does anything like this exist in the OpenGL/Glut libraries, or will I have to look elsewhere?

From stackoverflow
  • You can detect when a keypress event occurs, record that state, and then listen for a key release event.

    Wally : Which is what I am (sort of) currently doing. The problem is, glutKeyboardFunc() doesn't pass the current state. It only passes the ASCII character of the key.
    Scottie T : So keep track of the button you're interested in. You'll have to roll your own button state machine.
  • I have never used Glut, but I know that many people will say SDL is better. I have used SDL and I like it a lot. It does everything Glut does and a lot more. In SDL, you can use SDL_PollEvent() to get key state without the keyboard buffer delay.

    Edit: I know almost nothing about Glut, but it looks like you can use glutKeyboardFunc to detect normal keys, and glutSpecialFunc for keys that do not generate ASCII characters (such as shift). I'm not sure if there is a better way, as this doesn't seem very nice.

    Wally : Looks like this is what I'm going to have to do. SDL will fix other numerous problems as well. Thanks!

0 comments:

Post a Comment