PDA

View Full Version : question about glutKeyboardFunc


xDexx
2003.05.05, 01:16 AM
i set up this code:

in main

glutKeyboardFunc(checkNormKeys);


then in checkNormKeys()


if (key =='d')
scrollLeft();
else if(key == 'a')
scrollRight();
else if(key == 's')
scrollUp();
else if(key == 'w')
scrollDown();


the code works, but it only does one key at a time, how would i enable it to do two keys at once? like if 'd' makes a character move left and 'w' makes it move up, then they are both pressed at the sometime i want it to move up and left at the same time. any ideas?
-brett

Fenris
2003.05.05, 08:13 AM
By removing every 'else' from that function. :) It's all about flow control, man! *hippie-sounding*

OneSadCookie
2003.05.05, 08:48 AM
static bool keys[255];

...

void keyPressed(unsigned char k, int x, int y)
{
keys[k] = true;

if (keys['h'] && keys['i'])
{
printf("Both h & i are pressed!\n");
}
}

void keyReleased(unsigned char k, int x, int y)
{
keys[k] = false;
}

...

unsigned int i;
for (i = 0; i < 255; ++i)
{
keys[i] = 0;
}
glutKeyboardFunc(keyPressed);
glutKeyboardUpFunc(keyReleased);

(This is one of the flakiest points of GLUT, and will break down completely if the user holds a modifier key during either the up-stroke or the down-stroke of the key but not the other. You can get around this somewhat by "de-shifting" the keys yourself, but option and control will continue to be problematic.)

xDexx
2003.05.05, 12:01 PM
i took out the elses but it still didnt work so i tried onesadcookie's suggestion, it works great, i wish i could have come up with that! oh but by taking out the elses i dont need to test for both keys in an if statment :) thanks for your help!!
-brett