Archived

This topic is now archived and is closed to further replies.

fallingbrickwork

GLUT Keys??!? Scanning for two keys???

Recommended Posts

Hi All, Can you please help me with a small prob? How can I scan for two keyboard presses at the same time?? I''m using the `z` and `x` keys to controls a player ship and the `m` key to fire. If you fire (m key) whilst moving (z/x keys), the ship stops while it sorts out the firing routine and you have to repress the moving keys. How can I fire and move at the same time ?(i.e scan both keys presses). This is my simple key pressing GLUT Function void MyKeys (unsigned char key, int x, int y) { switch (key) { case 27: PostQuitMessage(0); break; case ''z'': PlayerSpeedAdder += 0.0003; break; case ''x'': PlayerSpeedAdder -= 0.0003; break; case ''m'': if (PlayerShotFlag == 0.0) { PlayerShotFlag = 1.0; PlayerPositionXOLD = PlayerPositionX; PlayerShotPositionZ = -2.0; } break; } } Is GLUT able to do what I want?? Cheers!!

Share this post


Link to post
Share on other sites
Even if multiple keys are pressed do they get reported one after another. You will have to check for both press/release and save the state in some variables. Here is a much better explanation http://www.fatech.com/tech/opengl/glut/

Remember that some keyboards can not report certain combination of keys.

Share this post


Link to post
Share on other sites
Well how are you getting your keys?
are you using window messages like wm_keydown and using wParam as your key variable ? if thats the case then try using the "GetAsyncKeyState(vkey);" api call "if your under windows, sorry i dont know the equivalent on u*nx" that should fix it .. just use the window messages to know if there is a key pressed if you dont want to keep it checking with every frame update.

what i think is wrong in your version is that your getting one unsigned char key at a time.. so you cant have two keys at the same time, then again i might be wrong as always .


Edited by - dizman on November 4, 2001 11:11:20 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
A better way to do it is just:

//Key states
unsigned char keys[256];

MyKeyDown(unsigned char key, int x, int y)
{
keys[key]=1; //Key hit.
}

MyKeyUp(unsigned char key, int x, int y)
{
keys[key]=0; //Key released.
}

void MyIdle(void)
{
if (keys[''z'']) //Do stuff
{
}
if (keys[''x'']) //do stuff
{
}
if (keys[''m'']) //do stuff
{
}
if (keys[27]) exit(0); // or PostQuitMessage(0);
//You can now have several things going on at once (multiple keys).
}

void ResetKeys(void)
{
for (int ctr=0;ctr!=256;++ctr) keys[ctr]=0; //Sets all keys to 0
}

int main(int argc, char **argv)
{
glutInit(&argc,argv);
//Other glut init and window stuff here.
ResetKeys();
procKeyboard(MyKeyDown); //Keyboard hit
procKeyboardUp(MyKeyUp); //Keyboard released.
procIdle(MyIdle);

}

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
By the way.. (that was my post)... I beleive that is more efficient (read, doesn''t require as much of the CPU) than GetKeyState, and isn''t windows specific either (which is the point of using OpenGL and glut).

Billy

BillyB@mrsnj.com -> if you need anything, email me.

Share this post


Link to post
Share on other sites