Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Keyboard::isKeyPressed()  (Read 4288 times)

0 Members and 1 Guest are viewing this topic.

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
sf::Keyboard::isKeyPressed()
« 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

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: sf::Keyboard::isKeyPressed()
« Reply #1 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.
« Last Edit: May 05, 2012, 01:16:55 am by Perde »

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Re: sf::Keyboard::isKeyPressed()
« Reply #2 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?

Paki Programmer

  • Newbie
  • *
  • Posts: 31
    • View Profile
Re: sf::Keyboard::isKeyPressed()
« Reply #3 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

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
Re: sf::Keyboard::isKeyPressed()
« Reply #4 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.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: sf::Keyboard::isKeyPressed()
« Reply #5 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)

Mars_999

  • Full Member
  • ***
  • Posts: 103
    • View Profile
    • Email
Re: sf::Keyboard::isKeyPressed()
« Reply #6 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.

 

anything