PDA

View Full Version : get keyboard input



vgm2
03-06-2001, 07:38 PM
Hi,
I am new to opengl, and I need some help
with how to get keyboard and mouse input.
Please somebody help.
Thanks

jc
03-07-2001, 12:00 AM
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(8); 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 http://www.opengl.org/discussion_boards/ubb/smile.gif

03-07-2001, 12:20 AM
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?

Bob
03-07-2001, 03:29 AM
Use flags to determine wether a key is pressed or not, and use the keyboard function to set the flags.



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();
...
}

outlander78
03-07-2001, 06:15 AM
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.

vgm2
03-13-2001, 05:17 PM
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.

vgm2
03-14-2001, 08:41 AM
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.

Hermann
03-14-2001, 12:17 PM
One question fist: what are you using? GLUT, win32, linux?

vgm2
03-14-2001, 02:18 PM
Originally posted by Hermann:
One question fist: what are you using? GLUT, win32, linux?
Hi,
Thanks for your reply, I am using Win32.