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

Author Topic: Only want a function to get called once on a keyDown.  (Read 12301 times)

0 Members and 1 Guest are viewing this topic.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Only want a function to get called once on a keyDown.
« Reply #15 on: January 26, 2012, 01:01:42 am »
I'm using a number of objects I iterate over (array of base class pointers to be specific) when I receive an input event. Each one has an "Update()" method that is called when receiving an input event, e.g. for keyboard inputs I've got this:
Code: [Select]
void KeyInput::Update(const sf::Event &event)
{
if ((event.Type == sf::Event::KeyPressed || event.Type == sf::Event::KeyReleased) && key == event.Key.Code)
{
Tapped = event.Type == sf::Event::KeyPressed && !Down;
Down = event.Type == sf::Event::KeyPressed;
}
}

For comparison, this is for gamepad/joystick buttons:
Code: [Select]
void JoyButtonInput::Update(const sf::Event &event)
{
if ((event.Type == sf::Event::JoystickButtonPressed || event.Type == sf::Event::JoystickButtonReleased) && controller == event.JoystickButton.JoystickId && button == event.JoystickButton.Button)
{
Tapped = event.Type == sf::Event::JoystickButtonPressed && !Down;
Down = event.Type == sf::Event::JoystickButtonPressed;
}
}

After this handling is done, I'm able to use the members "Tapped" and "Down" to determine whether some key has been pressed (still down) or tapped (one frame only). Once updating is done, I reset the "Tapped" member of all input objects to false, so it's no longer set while holding down the key/button.

It's probably not the most effective way to handle this, but it's very convenient to handle and sufficient for my use for now.

ingwik

  • Jr. Member
  • **
  • Posts: 65
    • View Profile
Only want a function to get called once on a keyDown.
« Reply #16 on: February 03, 2012, 08:52:53 am »
I had a similar problem with the animation from the wiki not working as intended.

Solution: http://www.sfml-dev.org/forum/viewtopic.php?t=5933

The solution was to use enumeration to check if it was already playing and only start an animation sequence if it was false. It was for SFML 1.6 though, even if I doubt it would make a difference.

 

anything