SFML community forums

Help => Window => Topic started by: JCSopko on June 30, 2010, 05:54:03 pm

Title: Execute Code on Key Release?
Post by: JCSopko on June 30, 2010, 05:54:03 pm
I've been looking around the forums and the documentation but have been unable to find a way to execute a string of code on the release of a key press with SFML 1.6. Can someone help me out or point me in the right direction? Thanks.

Regards,
Joe
Title: Execute Code on Key Release?
Post by: Laurent on June 30, 2010, 06:14:18 pm
What is a "string of code"?
Title: Execute Code on Key Release?
Post by: JCSopko on June 30, 2010, 06:58:49 pm
My bad, poor wording on my part, a few lines of code like setting a velocity variable to zero. So just replace a string of code with code, sorry for the confusion =]
Title: Execute Code on Key Release?
Post by: Spodi on June 30, 2010, 07:19:50 pm
Sounds like you need to learn the basics of programming. What you are looking for is conditional statements.
Title: Execute Code on Key Release?
Post by: Laurent on June 30, 2010, 07:20:27 pm
Quote
My bad, poor wording on my part, a few lines of code like setting a velocity variable to zero. So just replace a string of code with code, sorry for the confusion =]

Ok :)

So what's your problem exactly? Did you read the tutorial about events first?
Title: Execute Code on Key Release?
Post by: JCSopko on June 30, 2010, 07:34:05 pm
Yeah I've read through the tutorial and searched through the documentation but have been unable to find exactly what I'm looking for.

I'm using Box2D and am applying a force on key presses but I would like the linear velocity to be set to 0 when the key is released so the player is only moving when the key is held down.

Does SFML allow for this or is there a workaround?

Thanks,
Joe
Title: Execute Code on Key Release?
Post by: Laurent on June 30, 2010, 07:42:25 pm
Code: [Select]
// This is the event loop as described in the tutorials
sf::Event event
while (window.GetEvent(event))
{
    if (event.Type == sf::Event::KeyReleased && event.Key.Code == sf::Key::Space)
    {
        // set linear velocity to 0
    }
}


If you're rather applying your forces every frame as long as the key is held, you can use sf::Input instead
Code: [Select]
if (window.GetInput().IsKeyDown(sf::Key::Space))
{
    // apply forces
}
Title: Execute Code on Key Release?
Post by: JCSopko on June 30, 2010, 09:22:41 pm
I think I understand, but I have work so I don't have time to be sure or try to implement it, I'll try when I get back. Currently my code for momvement is as follows:

Code: [Select]
if(Game.GetInput().IsKeyDown(sf::Key::A))
{
if(myBox.body->GetLinearVelocity().x > 0)
{
myBox.yVel = myBox.body->GetLinearVelocity().y;
myBox.body->SetLinearVelocity(b2Vec2(0,myBox.yVel));
}

myBox.body->ApplyForce(b2Vec2(-75, 0), b2Vec2(myBox.position.x, myBox.position.y));
}


and that along with other input is in the game loop. so If i put the first example you showed me along with this code in the while loop it should stop all movement on the key release correct?

Thanks for all the help by the way, sorry this post is a little vague and rushed.

Joe