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

Author Topic: How do you make the keyboard just click one time? (SFML 2.0)  (Read 4340 times)

0 Members and 2 Guests are viewing this topic.

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
How do you make the keyboard just click one time? (SFML 2.0)
« on: November 04, 2012, 12:03:43 pm »
Hello  ;D

I have a problem with the keyboard input thing... i just want it to click 1 single time instead of 2 or 3 times when i click a key.

Heres my code... just so you can see what i mean. Also... Does key A count as "left mouse click"?... it looks like it does. (u can copy and paste if you wanna test)

#include <SFML/Graphics.hpp>
#include <sstream>
#include <string>

sf::RenderWindow mywindow(sf::VideoMode(800,600,32),"dsda");
sf::Event ev;

int x = 0;

template <typename T>
std::string toString(T arg)
{
        std::stringstream ss;
        ss << arg;
        return ss.str();
}

int main()
{
        sf::Text mytext;
        mywindow.setVerticalSyncEnabled(true);

        while(mywindow.isOpen())
        {
                mytext.setString(toString(x));
                while(mywindow.pollEvent(ev))
                {
                        if(ev.key.code == sf::Keyboard::A)
                        {
                                x++;
                        }
                }
                mywindow.clear(sf::Color(0,200,0));
                mywindow.draw(mytext);
                mywindow.display();

        }
}
 

FRex

  • Hero Member
  • *****
  • Posts: 1848
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: How do you make the keyboard just click one time? (SFML 2.0)
« Reply #1 on: November 04, 2012, 02:05:52 pm »
This is stressed so much in the tutorial that Laurent should have started French Baguette Massacre by this point :
Quote
The valid member is the one that matches the event type, for example event.key for a KeyPressed event. Trying to read any other member will result in an undefined behaviour (most likely: random or invalid values). So never try to use an event member that doesn't match its type.
(...)
Read the above paragraph once again and make sure that it's printed in your head, the sf::Event union causes too many problems to inadvertent programmers.
:-X
Back to C++ gamedev with SFML in May 2023

Assassinbeast

  • Jr. Member
  • **
  • Posts: 62
    • View Profile
    • Email
Re: How do you make the keyboard just click one time? (SFML 2.0)
« Reply #2 on: November 04, 2012, 03:10:08 pm »
sorry, forgot there were tutorials  ::)

kaB00M

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Caffeware
    • Email
Re: How do you make the keyboard just click one time? (SFML 2.0)
« Reply #3 on: November 04, 2012, 11:23:40 pm »
You can use something like:

bool key_triggered[sf::Keyboard::KeyCount]; //array containing all possible keys

void resetTriggered()
{
    for (int i = 0; i < sf::Keyboard::KeyCount; ++i)
        key_triggered[i] = false;
}

//initialize!
ResetTriggered();

bool isKeyTrigger(sf::Keyboard::Key key)
{
    if ( sf::Keyboard::isKeyPressed(key) && !key_triggered[key] )
    {
        return (key_triggered[sf::Keyboard::Right] = true);
    }
}
 

Now, you can use it like this.

if (isKeyTrigger(sf::Keyboard::Right)) //your choice of key
{
    //example: move character right!
}

//must update after end of key-check loop!
resetTriggered();
 

This is a simplified version; you can insert the variables and fuction inside a class to make it more uniform.
Also, note that you can check for the same key ONLY ONCE per frame/tick.



 

anything