PDA

View Full Version : how execute instructions when there is no events/no key pressed ?



airseb
05-28-2003, 09:09 AM
hi !
i have a problem, i would like to execute instructions when no key are pressed.
What library can i use with open gl and what instruction make it ?
i have tried to use glutIdleFunc () but i don't find where put this function.
i have done this with it but it works only when i press a key and not when no key are pressed : (thanks for your help)



void keyboard (unsigned char key, int x, int y)
{
switch (key)
{
case 'r' :
case 'R' :
nb_transformation++ ; //compte le nombre de transformations pour ensuite stabiliser la trainée
r = 5.0;
glutPostRedisplay () ; //réexécute la fonction display
break ;

default :
glutIdleFunc(withoutEvents) ;
break ;
}
}

dabeav
05-28-2003, 09:14 AM
Simple, your keyboard function is ONLY called when a key is pressed, so you need another function. This will work, simply create a bool table like bool keys[256]; then when a key is pressed in the keyboard func set the key to possitive like key = 13; set keys[13] = true; etc; (remember to set it back to negative when you let go of hte key with a keyboard up function). Then in another function, each frame check to see if any of hte keys are true, if not then run your code from there. Get it?

airseb
05-28-2003, 09:32 AM
Originally posted by dabeav:
Simple, your keyboard function is ONLY called when a key is pressed, so you need another function. This will work, simply create a bool table like bool keys[256]; then when a key is pressed in the keyboard func set the key to possitive like key = 13; set keys[13] = true; etc; (remember to set it back to negative when you let go of hte key with a keyboard up function). Then in another function, each frame check to see if any of hte keys are true, if not then run your code from there. Get it?

sorry i haven't all understood.
I have to create bool array of 256 elements and then i don't understand :-( What is "hte" ?
can you explain to me once of more please ?


[This message has been edited by airseb (edited 05-28-2003).]

dabeav
05-28-2003, 09:44 AM
Ok here is some code:

//Need a bool list
bool KeyDown[256];

//Need to set all the bools to false to start with
for(int a=0; a<256; a++)
{
KeyDown[a] = false;
}

//Keyboard DOWN function
void keyboard (unsigned char key, int x, int y)
{
//Add this to the existing keyboard function
KeyDown[key] = true;

//Put the rest of your code here, BUT, a good idea would be to simply use the bool list in another function for ANYTHING that requires a key to activate or unactivate, because sometimes glut call backs can be slow, and using a bool check each frame would be faster for a key we already know is still down.
}

//Keyboard UP function
void keyboardup (unsigned char key, int x, int y)
{
//Add this to the existing keyboard function
KeyDown[key] = false;
}

//Now check this function each frame for keys that are down
void DoStuffWhenNoKeyPressed(void)
{
//Bool check to see if we want to go on
bool GoOn = true;

//Check all the keys for presses
for(int a=0; a<256; a++)
{
if(KeyDown[a] == true)
{
//ifpressed break out of loop and DONOT go on
GoOn = false;
break;
}
}

//If we want to go on, then run your code you want that requires NO key presses here
if(GoOn)
{
//Yada yada
}
}


Get it?

[This message has been edited by dabeav (edited 05-28-2003).]

airseb
05-28-2003, 11:22 AM
thanks for your help !
i'll try this in few days because i can't earlier and i'll post here if i don't manage.

airseb
05-28-2003, 12:05 PM
i have a question : if the variable "key" is a letter will it work ?

dabeav
05-29-2003, 03:09 AM
Yes, because the char value returned is just an ascii letter code, like i think enter = 13, c = 67 or 99 depending on the caps lock. etc; so yes the a char key will work as well, that is why the buffer is 256 that allows for all keys plus a buffer of a couple keys like if you wanted to use the mouse as a key etc, (but that is a whole other topic).

M/\dm/\n
05-29-2003, 03:17 AM
Idle function should work, works for me!
If I remeber correct:
at the beginning of file:
void spin(void);

anywhere in render path, but prefferably once for specific event for ex. mousepress;
if (mousekey==MK_LEFT)
glutIdleFunc(spin);

void spin(void)
{
glRot...(i++;0,0,1)
draw quad for ex.
}