SFML community forums

General => General discussions => Topic started by: Mars_999 on May 05, 2012, 12:04:21 am

Title: sf::Keyboard::isKeyPressed()
Post by: Mars_999 on May 05, 2012, 12:04:21 am
I am trying to use the space bar to allow user to move forward until they hit it again, but I am not having any luck with it. The problem I am sure is the logic, but can't figure out how else to do it...

if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
SetMoveFwd(!IsMoveFwd());

Thanks!

BTW SFML2.0
Title: Re: sf::Keyboard::isKeyPressed()
Post by: Perde on May 05, 2012, 01:08:07 am
My wild guess would be that isKeyPressed returns true as long as the key is pressed, and therefore SetMoveFwd is called numerous times till you release the key again (let's assume this happens in a (game) loop). That would result in a kinda random state of whatever SetMoveFwd changes (constantly switching from true to false and so on), unless you're not showing everything.

It's just a guess though.
Title: Re: sf::Keyboard::isKeyPressed()
Post by: Mars_999 on May 05, 2012, 01:20:56 am
My wild guess would be that isKeyPressed returns true as long as the key is pressed, and therefore SetMoveFwd is called numerous times till you release the key again (let's assume this happens in a (game) loop). That would result in a kinda random state of whatever SetMoveFwd changes (constantly switching from true to false and so on), unless you're not showing everything.

It's just a guess though.

that is my guess also but what to do about it?
Title: Re: sf::Keyboard::isKeyPressed()
Post by: Paki Programmer on May 05, 2012, 01:26:31 am
maybe make it event based by using ::isKeyReleased() i think? I've never done it before my self because I never had the need but I think that should get you started on the right track. Have the function detect that if the last key released was the space bar, then to set the movement to false. Someone can correct me if I'm wrong
Title: Re: sf::Keyboard::isKeyPressed()
Post by: Groogy on May 05, 2012, 02:10:39 am
Well what you want to do is more or less an event. Let's say it in english and then look from it from a programmers perspective.

You are waiting for the user to release space. do stuff and then on the next release do other stuff. What you are currently doing is querying is the space pressed.

More or less, you want to use the events from sf::Window::pollEvent ;)
sf::Event and sf::Keyboard have two very different intended uses.
Title: Re: sf::Keyboard::isKeyPressed()
Post by: Perde on May 05, 2012, 02:23:07 am
You should really use events, just like Groogy said.

(Anyway, your method can be fixed by a simple flag that prevents calling your method a second time unless the key has been released in the meantime. No seriously, don't do it that way.  ;D)
Title: Re: sf::Keyboard::isKeyPressed()
Post by: Mars_999 on May 05, 2012, 03:38:15 am
Kind of what I figured I needed something that was triggered when an action happened vs. constantly updating each loop....

Thanks I will give it a go.