Results 1 to 9 of 9

Thread: get keyboard input

  1. #1
    Junior Member Newbie
    Join Date
    Mar 2001
    Posts
    6

    get keyboard input

    Hi,
    I am new to opengl, and I need some help
    with how to get keyboard and mouse input.
    Please somebody help.
    Thanks

  2. #2
    Member Contributor
    Join Date
    Aug 2000
    Location
    france
    Posts
    89

    Re: get keyboard input

    Hi,

    If u want to use the keyboard (with the glut), u have the function

    glutKeyboardFunc(MyKeyboardFunc);

    that u should add to you'r opengl init function.

    The keyboard func should look like this :

    void MyKeyboardFunc(unsigned char Key, int x, int y)
    {
    switch(Key)
    {
    case 'z': MenuHandler(0); break;
    case 'x': MenuHandler(1); break;
    case 'c': MenuHandler(2); break;
    case 'v': MenuHandler(3); break;
    case 'w': MenuHandler(11); break;
    case 'b': MenuHandler(12); break;
    case 's': MenuHandler(7); break;
    case 'e': MenuHandler(; break;
    case ' ': if (LeftButtonDown)
    if (RightButtonDown) RightButtonDown=0; else RightButtonDown=1;
    break;
    case 27:
    exit(1);
    break;
    };
    }

    There is the same thing with the mouse:
    Add "glutMouseFunc(MyMouseFunc)" in the opengl init func.

    void MyMouseFunc(int button, int state, int x, int y)
    {
    switch(button)
    {
    case GLUT_LEFT_BUTTON:
    if (state==GLUT_DOWN) // LEFT-BUTTON DOWN
    {
    OldX=x; OldY=y; // RESET THE OLD TO THE CURRENT (starting over)
    LeftButtonDown=1;
    RightButtonDown=0;
    glutDetachMenu(GLUT_RIGHT_BUTTON);
    InertiaOn=0;
    }
    else // LEFT-BUTTON UP
    {
    LeftButtonDown=0;
    glutSetMenu(MainMenuID);
    glutAttachMenu(GLUT_RIGHT_BUTTON);

    // IF USER HAS MOVED ENOUGH (MINMOVE), INVOKE INERTIAL SYSTEM
    if ( (ABS(NewX-OldX) >= MINMOVE) | | (ABS(NewY-OldY) >= MINMOVE) )
    InertiaOn=1;
    }
    break;
    case GLUT_RIGHT_BUTTON:
    if (state==GLUT_DOWN) RightButtonDown=1; else RightButtonDown=0;
    break;
    };
    }

    Hope that will help u

  3. #3
    Guest

    Re: get keyboard input

    How about using the keyboard as user input to manipulate shapes in the program? Whenever I use glutKeyboardFunc or glutSpecialFunc to do this, I get jumpy movement as these functions only call when a key is struck (or when the key repeats when it is held for a while). How do I get fluid movement out of opengl when reading from the keyboard?

  4. #4
    Senior Member OpenGL Guru
    Join Date
    Feb 2000
    Location
    Sweden
    Posts
    2,978

    Re: get keyboard input

    Use flags to determine wether a key is pressed or not, and use the keyboard function to set the flags.
    Code :
    a_key_down = false;
     
    MyKeyboardFunc()
    {
    ...
    if(key A is pressed) a_key_down = true;
    if(key A is released) a_key_down = false;
    ...
    }
     
    MyIdleFunc()
    {
    ...
    if(a_key_down == true) MoveObjects();
    glutPostRedisplay();
    ...
    }

  5. #5
    Member Newbie
    Join Date
    Dec 2000
    Location
    Canada
    Posts
    34

    Re: get keyboard input

    Go check out NeHe's tutorials (linked to from this site). IIRC, one explains how to use DirectX's DirectInput to get keystrokes and mouse movements.

  6. #6
    Junior Member Newbie
    Join Date
    Mar 2001
    Posts
    6

    Re: get keyboard input

    Originally posted by vgm2:
    Hi,
    I am new to opengl, and I need some help
    with how to get keyboard and mouse input.
    Please somebody help.
    Thanks
    Thanks for your reply.
    I would like to know if the MenuHandler function arguments are the asci values of the
    characters. If not could you please tell me where I can get those value?
    Thanks.

  7. #7
    Junior Member Newbie
    Join Date
    Mar 2001
    Posts
    6

    Re: get keyboard input

    Hi again,
    I think I asked the wrong question. My intention was to ask if there is an operator
    in opengl to handle input like in a C++ console application.
    For example:
    int k;
    cout<<"\nPlease enter an integer\n";
    cin>>k;
    k will be stored in memory as an integer
    and will be accessed when desired.
    Please if there is a way, can someone tell me?
    Thanks for your time.

  8. #8
    Junior Member Newbie
    Join Date
    Feb 2001
    Posts
    6

    Re: get keyboard input

    One question fist: what are you using? GLUT, win32, linux?

  9. #9
    Junior Member Newbie
    Join Date
    Mar 2001
    Posts
    6

    Re: get keyboard input

    Originally posted by Hermann:
    One question fist: what are you using? GLUT, win32, linux?
    Hi,
    Thanks for your reply, I am using Win32.

Similar Threads

  1. Keyboard input
    By in forum OpenGL: Basic Coding
    Replies: 2
    Last Post: 12-25-2003, 11:00 PM
  2. Keyboard Input
    By in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 04-26-2003, 02:01 PM
  3. VB - Keyboard Input
    By Bhaji123Go in forum OpenGL: Advanced Coding
    Replies: 0
    Last Post: 11-08-2002, 02:34 PM
  4. Using Keyboard input?
    By GarlicGL in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 11-07-2001, 08:19 AM
  5. keyboard input
    By Vlasko in forum OpenGL: Basic Coding
    Replies: 3
    Last Post: 09-21-2001, 05:34 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