PDA

View Full Version : glutKeyboardFunc takes 5 frames to update state



shotoreaper
03-14-2013, 03:02 PM
Hi!
I have the keyboard class that initialize the glutKeyboards functions. When I release a key, the program takes five frames to detect. I have another class that when I push the key 's', it add a vec3(1.0,1.0,1.0) to the variable position. The problem is that in each frame from that I press key 's' until it detects that I released it adds to the variable position the vector vec3(1.0,1.0,1.0). The update() function is called before the Draw () function in the render method

KeyboardDevice.cpp


void KeyboardDevice::Initialize()
{
setVisible(false);
setService(true,2);
// Initialize glut keyboard functions
glutKeyboardFunc(keyPressed);
glutKeyboardUpFunc(keyUp);
glutSpecialFunc(keySpecial); // Tell GLUT to use the method "keySpecial" for special key presses
glutSpecialUpFunc(keySpecialUp); // Tell GLUT to use the method "keySpecialUp" for special up key events
}
void KeyboardDevice::Load()
{

}

void KeyboardDevice::Update(){}
void KeyboardDevice::Draw(){}

void KeyboardDevice::keyPressed (unsigned char key, int x, int y)
{
keyStates[key] = true; // Set the state of the current key to pressed
}
void KeyboardDevice::keyUp (unsigned char key, int x, int y) // it takes 5 frames to detect the release after i press the key.
{
keyStates[key] = false; // Set the state of the current key to not pressed
timeReleased = clock();
keyReleaseStates[key] = true; // Set the state of the current key recently pressed
}
void KeyboardDevice::keySpecial(int key, int x, int y)
{
keySpecialStates[key] = true;
}
void KeyboardDevice::keySpecialUp(int key, int x, int y)
{
keySpecialStates[key] = false;
timeReleased = clock();
keySpecialReleaseStates[key] = true; // Set the state of the current special key recently pressed
}

bool KeyboardDevice::IsKeyPress(unsigned char key)
{
return keyStates[key];
}
bool KeyboardDevice::IsKeyUp(unsigned char key)
{
double now = clock()/(double)CLOCKS_PER_SEC;
double last = timeReleased /(double)CLOCKS_PER_SEC;
if(now-last < 1 && keyReleaseStates[key] == true)
{
keyReleaseStates[key] = false;
return true;
}
return false;
}

Actor.cpp


void Cube::Update(void)
{
if(kb->IsKeyPress('s'))
position += glm::vec3(1.0 * getParent()->getEngine()->deltaTime);
}

Any idea to resolve this?

V-man
03-15-2013, 06:13 AM
Why don't you perform the calculation as soon as the keyboard button is pressed?