Results 1 to 6 of 6

Thread: open GL keyboard func

  1. #1
    Member Contributor
    Join Date
    May 2012
    Posts
    51

    open GL keyboard func

    How can I listen for the keys for UP, DOWN, LEFT and RIGHT?
    When I ran a test on this using the function below, I fount it hard to display the keys and I don't know their ascii values.

    Anybody help?

    Code :
    void KeyDown(unsigned char key, int x, int y)
    {	cout <<key<<endl;
    }

  2. #2
    Senior Member OpenGL Lord
    Join Date
    Mar 2015
    Posts
    6,671

    Re: open GL keyboard func

    1: This is about GLUT, not OpenGL. They are not the same thing.

    2: If you want to display the ASCII value of a key, you need to convert it into an integer with a cast operation.

  3. #3
    Member Contributor
    Join Date
    Feb 2011
    Posts
    72

    Re: open GL keyboard func

    use glut
    im not sure if u know but
    there are more than one function for each device sometimes
    depending on what they do


    init();

    glutDisplayFunc(display);
    glutIdleFunc(display);
    glutReshapeFunc(reshape);


    /* MOUSE */
    glutWarpPointer(mid_x, mid_y);
    glutSetCursor(GLUT_CURSOR_NONE);

    glutMouseFunc(mousePress);

    glutMotionFunc(mouseMove);
    glutPassiveMotionFunc(mouseMove); //check for mouse movement


    /* KEYS */
    glutSetKeyRepeat(GLUT_KEY_REPEAT_OFF);

    glutKeyboardFunc(keyboard);
    glutKeyboardUpFunc(keyboardup);

    glutSpecialFunc(specialkeyboard);
    glutSpecialUpFunc(specialkeyboardup);



    glutMainLoop();






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


    //glutIgnoreKeyRepeat(1);


    /* avoid thrashing this procedure */
    usleep(100);


    int tex = 1;


    switch (key) {

    case ESCAPE: // kill everything.

    exit(1);
    break; // redundant.


    case 'a': case 'A': // switch the blending

    ambOn = ambOn ? 0 : 1;

    printf("amb is now: %d\n", ambOn);
    break;



    case 'd': case 'D': // switch the blending

    difOn = difOn ? 0 : 1;


    printf("dif is now: %d\n", difOn);
    break;

    /*
    case 'b': case 'B': // switch the blending

    blend = blend ? 0 : 1;

    if (blend) {
    glEnable(GL_BLEND);
    glDisable(GL_DEPTH_TEST);
    } else {
    glDisable(GL_BLEND);
    glEnable(GL_DEPTH_TEST);
    }

    printf("Blending is now: %d\n", blend);
    break;
    */

    case 't': case 'T': // switch the blending

    tex = tex ? 0 : 1;

    if (!tex) {
    glEnable(GL_TEXTURE_2D);
    } else {
    glDisable(GL_TEXTURE_2D);
    }

    printf("Textures is now: %d\n", tex);
    break;



    case 'l': case 'L': // switch the lighting

    light = light ? 0 : 1;

    if (light) glEnable(GL_LIGHTING);
    else glDisable(GL_LIGHTING);

    printf("Lighting is now: %d\n", light);
    break;


    case '-': case '_': // switch the lighting

    if (light_intensity <= 0) return;

    light_intensity -= 0.1;
    break;


    case '=': case '+': // switch the lighting

    if (light_intensity >= 1) return;

    light_intensity += 0.1;
    break;



    case '{': case '[': // switch the lighting

    if (light_id <= 0) return;

    light_id -= 1;
    break;


    case '}': case ']': // switch the lighting

    //if (light_id >= 1) return;

    light_id += 1;
    break;

    /*
    case '.': // switch the lighting

    //if (light_id >= 1) return;

    f0 += 5;
    break;


    case ',': // switch the lighting

    //if (light_id >= 1) return;

    f0 -= 5;
    break;



    case 'm': // switch the lighting

    //if (light_id >= 1) return;

    f1 += 5;
    break;


    case 'n': // switch the lighting

    //if (light_id >= 1) return;

    f1 -= 5;
    break;


    case 'b': // switch the lighting

    //if (light_id >= 1) return;

    f2 += 5;
    break;


    case 'v': // switch the lighting

    //if (light_id >= 1) return;

    f2 -= 5;
    break;


    */

    case 'v': case 'V': // switch the lighting

    camera.perspective = camera.perspective ? 0 : 1;

    printf("camera.perspective is now: %d\n", camera.perspective);
    break;


    case 'c': case 'C': // drop crate

    dropCrate();
    break;



    default:

    printf ("Key %d pressed. No action there yet.\n", key);
    break;

    }

    keyboard_state = 0;

    }

  4. #4
    Newbie OpenGL Pro
    Join Date
    Jan 2007
    Posts
    1,789

    Re: open GL keyboard func

    In other words:
    • There is no OpenGL keyboard function.
    • OpenGL does not handle keyboard input.
    • OpenGL does not know the difference between a keyboard and an elephant.
    • OpenGL does not know the difference between a keyboard and the planet Mars.
    • OpenGL provides functions for interfacing with your graphics hardware, nothing else. Page 1, line 1 of any OpenGL manual.
    • GLUT provides some simple keyboard functions.
    • GLUT is a library built on top of OpenGL for the purposes of simplifying code in demos and tutorials.
    • GLUT is not OpenGL.

    This really should be a FAQ entry...

  5. #5
    V-man
    Guest

  6. #6
    Member Contributor
    Join Date
    Feb 2011
    Posts
    72

    Re: open GL keyboard func

    omfg rub it in jfc
    or u can get a dirt bike a street bike a skate board surf board and a snowboard one mountain bike a drumset and a bass guitar and get the f outside and enjoy the sun
    jk
    loling is ok
    free country!
    usa!

Similar Threads

  1. how to insert keyboard func in opengl prog
    By abhishek21 in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 05-04-2015, 05:31 AM
  2. help with my camera func
    By ShinGouki in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 05-08-2003, 09:27 PM
  3. Blend Func
    By MrShoe in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-06-2001, 06:51 PM
  4. Keyboard: how can i read the buffer of the keyboard ?
    By Bahamut_france in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 03-02-2001, 08:13 AM
  5. Help needed with keyboard Func with multiple windows
    By in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 03-21-2000, 04:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean