0
votes

I'm detecting key pressing and have some problem with the detection with fn(function), ctrl(control), alt and fn(function).

I can detect space bar, enter, ESC, letters, numbers and the cursors. But i can't detect the special keys I tell before. Both method doesn't show anything on screen when I press these keys.

Method to detect normal keys

void KeyboardFunc(unsigned char key, int x, int y)
{
    int numeros;

    Key[key] = true;

    cout << "-----------------" << endl;
    cout << "PULSE " << (int) key << endl;
    cout << "-----------------" << endl;

    if(key >= 48 && key < 58)
    {
        numeros = key; 
        key = 'a';  
    }

    switch ( key ) 
    {
        case ' ':   // Space bar
            cout << "Barra Espaciadora pulsada." << endl;
            break;
        case 13:    // #13 es Enter
            cout << "Enter pulsada." << endl;
            break;   
        case 27:    // "27" is the Escape key
            cout << "Tecla ESC pulsada." << endl;
            exit(1);
        case 'a':   // Teclado numérico.
            cout << "Número pulsado: " << numeros - 48 << endl;
            key = numeros;    
    }
}

Method to detect special keys:

void SpecialFunc(int key, int x, int y)
{
    Arrow[key] = true;

    cout << "-----------------" << endl;
    cout << "ESPECIAL PULSADO: " << (int) key << endl;
    cout << "-----------------" << endl;

    switch ( key ) 
    {
        case GLUT_KEY_UP:    // #73 es cursor arriba
            cout << "Cursor ARRIBA pulsada." << endl;
            break;
        case GLUT_KEY_DOWN:    // #81 es cursor abajo
            cout << "Cursor ABAJO pulsada." << endl;
            break;
        case GLUT_KEY_LEFT:    // #73 es cursor izquierda
            cout << "Cursor IZQUIERDA pulsada." << endl;
            break;
        case GLUT_KEY_RIGHT:    // #81 es cursor derecha
            cout << "Cursor DERECHA pulsada." << endl;
            break;    
        case 30:    // #30 es alt_a     
            cout << "Tecla ALT pulsada." << endl;
            break;    
    }
}
3

3 Answers

4
votes

OpenGL does not deal with user input. It's a drawing API and only covers getting points, lines and triangles into a raster pixmap buffer.

What you are using right now is GLUT, a rather simple framework for small OpenGL demos. GLUT is not part of OpenGL! If GLUT no longer meets your demands it's time to go on using a more capable framework, or doing all the windowing and input processing from scratch.

1
votes

You could switch to SDL - get a source for brutalchess,

http://brutalchess.sourceforge.net/

and see how keys are handled in it. datenwolf is right, opengl has nothing to do with input ( mouse, keys, window focus etc. ). You could also try SFML, it's simpler than SDL

http://rastergrid.com/blog/downloads/mountains-demo/

SDL is more flexible than SFML, e.g. KEY_PRESS is something different than KEY_RELEASE.

0
votes

Ok so this post is old I know but I just happened to find it while looking for something else.

Answering anyway since it may help other people in the future.

GLUT actually supports the events of ALT... being pressed.

GLUT_ACTIVE_SHIFT – Set if either you press the SHIFT key, or Caps Lock is on. Note that if they are both on then the constant is not set.

GLUT_ACTIVE_CTRL – Set if you press the CTRL key.

GLUT_ACTIVE_ALT – Set if you press the ALT key.

All you have to do is:

void processNormalKeys(unsigned char key, int x, int y) {

if (key == 27)
    exit(0);
else if (key=='r') {
    int mod = glutGetModifiers();
    if (mod == GLUT_ACTIVE_ALT)
        //piece of code
    else if(//...)
        //piece of code
}}