Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 3 of 3

Thread: Weird! No response to key press

  1. #1
    Junior Member Newbie
    Join Date
    Sep 2001
    Location
    Reno, Nevada, US
    Posts
    4

    Weird! No response to key press

    I am working on a programming assign, a 2D Pong game. I finished it on my home PC (WindowsME) in VC++, using only GLUT to implement the interface.

    And then I sent it to a SGI workstation (Unix) in our computer lab, the compilation and linking was successful, the executable could run, the appearence is fine, and it could respond to my first key strokes, but no response to key strokes afterward, the animation is fine, the menu events are fine, text display is fine, everything is fine but the key strokes event.

    I tested other simple key test programs on those SGI workstations, they worked, what's wrong with my Pong program?

    Please help me. If you need the whole source code, please leave your email, I will send t you. Thanks!!!

    my main() and key callback funcs
    -------------------------------------------
    //--------------------------------------------------------
    // The main() entry
    //--------------------------------------------------------

    int main(int argc, char** argv)
    {

    // Initialize GL and GLUT, create default window
    glutInit(&argc,argv);
    glutInitWindowSize( WIN_WIDTH, WIN_HEIGHT );

    glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
    glutCreateWindow("2D Pong");

    myInit ();
    createPopMenu();

    glutDisplayFunc(myDisplay);
    glutKeyboardFunc(myKey);
    glutSpecialFunc(mySpecialKey);
    glutReshapeFunc(myReshape);
    glutIdleFunc(myIdle);

    glutMainLoop();

    return 0;
    }

    static void mySpecialKey(int key, int x, int y)
    {
    switch (key) {
    case GLUT_KEY_F2:
    doRestart();
    break;
    case GLUT_KEY_F4:
    doStop();
    break;
    case GLUT_KEY_UP:
    if (paddle2.y4 >= POOL_HEIGHT - PADDLE_HEIGHT / 2)
    {
    paddle2.y1 = POOL_HEIGHT - PADDLE_HEIGHT * 2;
    paddle2.y2 = POOL_HEIGHT - PADDLE_HEIGHT * 2;
    paddle2.y3 = POOL_HEIGHT;
    paddle2.y4 = POOL_HEIGHT;
    }
    else
    {
    paddle2.y1 = paddle2.y1 + 25;
    paddle2.y2 = paddle2.y2 + 25;
    paddle2.y3 = paddle2.y3 + 25;
    paddle2.y4 = paddle2.y4 + 25;
    if (!started && Xpos > POOL_WIDTH / 2.0 )
    Ypos += 25;
    }
    break;
    case GLUT_KEY_DOWN:
    if (paddle2.y1 <= PADDLE_HEIGHT / 2)
    {
    paddle2.y1 = 0.0;
    paddle2.y2 = 0.0;
    paddle2.y3 = PADDLE_HEIGHT * 2.0;
    paddle2.y4 = PADDLE_HEIGHT * 2.0;
    }
    else
    {
    paddle2.y1 = paddle2.y1 - 25;
    paddle2.y2 = paddle2.y2 - 25;
    paddle2.y3 = paddle2.y3 - 25;
    paddle2.y4 = paddle2.y4 - 25;
    if (!started && Xpos > POOL_WIDTH / 2.0 )
    Ypos -= 25;
    }
    break;
    }
    glutPostRedisplay();
    }

    static void myKey(unsigned char key, int x, int y)
    {
    switch (key)
    {
    case 13: //[RETURN] Player 2 serve
    if (Xpos > POOL_WIDTH / 2.0)
    doStart();
    break;
    case 'S': //Player 1 serve
    case 's':
    if (Xpos < POOL_WIDTH / 2.0)
    doStart();
    break;
    case 27: //[ESC]
    exit(1);
    break;
    case 'q':
    exit(1);
    break;
    case 'a':
    case 'A':
    if (paddle1.y4 >= POOL_HEIGHT - PADDLE_HEIGHT / 2)
    {
    paddle1.y1 = POOL_HEIGHT - PADDLE_HEIGHT * 2;
    paddle1.y2 = POOL_HEIGHT - PADDLE_HEIGHT * 2;
    paddle1.y3 = POOL_HEIGHT;
    paddle1.y4 = POOL_HEIGHT;
    }
    else
    {
    paddle1.y1 = paddle1.y1 + 25;
    paddle1.y2 = paddle1.y2 + 25;
    paddle1.y3 = paddle1.y3 + 25;
    paddle1.y4 = paddle1.y4 + 25;

    if (!started && Xpos < POOL_WIDTH / 2.0 )
    Ypos += 25;
    }
    glutPostRedisplay();
    break;
    case 'Z':
    case 'z':
    if (paddle1.y1 <= PADDLE_HEIGHT / 2)
    {
    paddle1.y1 = 0.0;
    paddle1.y2 = 0.0;
    paddle1.y3 = PADDLE_HEIGHT * 2.0;
    paddle1.y4 = PADDLE_HEIGHT * 2.0;
    }
    else
    {
    paddle1.y1 = paddle1.y1 - 25;
    paddle1.y2 = paddle1.y2 - 25;
    paddle1.y3 = paddle1.y3 - 25;
    paddle1.y4 = paddle1.y4 - 25;

    if (!started && Xpos < POOL_WIDTH / 2.0 )
    Ypos -= 25;
    }

    glutPostRedisplay();
    break;
    }
    }

  2. #2
    Intern Contributor
    Join Date
    Jun 2001
    Location
    Montmagny, france
    Posts
    66

    Re: Weird! No response to key press

    You say that this work for the first key you hit but not after.
    => have you test to add this to the end of your myKey() and mySpecialKey() ?

    glutKeyboardFunc(myKey);
    glutSpecialFunc(mySpecialKey);


    @+
    Cyclone

  3. #3
    Junior Member Newbie
    Join Date
    Sep 2001
    Location
    Reno, Nevada, US
    Posts
    4

    Re: Weird! No response to key press

    Thanks for replying.
    I did what you said today, but the problem is still there. Actually, before I hit any valid keys, pressing other keys not captured in myKey() really triggered the callback func, but after fthe first valid key stroke, any key press does not trigger myKey(). Weird! What can I do? :-(

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •