question about glutKeyboardFunc

xDexx
Unregistered
 
Post: #1
i set up this code:

in main
Code:
glutKeyboardFunc(checkNormKeys);

then in checkNormKeys()

Code:
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
Quote this message in a reply
Oldtimer
Posts: 832
Joined: 2002.09
Post: #2
By removing every 'else' from that function. Smile It's all about flow control, man! *hippie-sounding*
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #3
Code:
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.)
Quote this message in a reply
xDexx
Unregistered
 
Post: #4
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 Smile thanks for your help!!
-brett
Quote this message in a reply
Post Reply