keypress issues with glut

If anyone knows much about openGL/ glut i'd appreciate the help.


1
2
3
4
5
6
7
8
9
10
void handleKeypress(unsigned char key, int x, int y)
{
     if(key == 27) //27 is escape
     {
         exit(0);
      }
}

//in main
glutHandleKeypress(handleKeypress);


Escape works and the program closes. But nothing else works. If i swap 27 for any other key. Eg VK_UP for the up key on windows. the program does not exit.
(windows.h is included)

Also if i swap what escape does.
Eg:
1
2
3
4
if(key == 27) 
     {
         std::cout << " Keypress";
      }


It doesnt output, the only thing i can get to work is escape to close the window. Am i doing soemthing wrong?
Last edited on
One thing I can say is that comparing it with VK_* doesn't make much sense because it's a different encoding. VK_KEY_A does correspond to ASCII value for 'A' (capital A), but the comparison wouldn't work for lowercase 'a', so be sure to use toupper/tolower if you want to detect the key press, and not a specific character.

Anyway, glutKeyboardFunc is only for ASCII-generating characters: http://www.opengl.org/resources/libraries/glut/spec3/node49.html (escape, backspace and delete). The buttons such as arrow keys are processed by glutSpecialFunc http://www.opengl.org/resources/libraries/glut/spec3/node54.html . Again, note that VK_KEY_* are not the ones to compare against - it's GLUT_KEY_*.

The reason why it doesn't output anything if you change exit(0) to std::cout << may hide in the fact that the buffer is not flushed. Try changing it to std::cout << " Keypress" << std::endl; or call std::cout.flush(); manually.
Topic archived. No new replies allowed.