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

Author Topic: How to detect a single buttonpress?  (Read 2432 times)

0 Members and 1 Guest are viewing this topic.

Teras23

  • Newbie
  • *
  • Posts: 7
    • View Profile
How to detect a single buttonpress?
« on: November 10, 2012, 07:22:39 pm »
I need to find a way that returns something or changes a value by 1 in a single key/button press, but everything I tried still does it every frame. I know would have to use the pressed and released event but I just cant figure out how. I also searched on the internet for the answer but I did not find anything.

Teras23

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to detect a single buttonpress?
« Reply #1 on: November 10, 2012, 07:45:54 pm »
Posted this in the wrong place, didn't I :-[ .

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to detect a single buttonpress?
« Reply #2 on: November 10, 2012, 09:42:38 pm »
It would have been enough if you had looked at the tutorials. sf::Event is what you need.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Teras23

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to detect a single buttonpress?
« Reply #3 on: November 10, 2012, 10:17:22 pm »
It would have been enough if you had looked at the tutorials. sf::Event is what you need.

I had looked at the tutorials but none of that stuff helped me, because it still checks is the mouse is down every frame and therefor do something every frame.

Teras23

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to detect a single buttonpress?
« Reply #4 on: November 10, 2012, 10:51:01 pm »
So after looking at the events agand and thinking for another 30 min I finally figured it out.
        bool pressed;
        sf::Event Event;
        if(Event.type == sf::Event::MouseButtonPressed && Event.mouseButton.button == sf::Mouse::Right && !pressed)
        {
            the thing to do here;
                pressed = true;
        }
        if(Event.type == sf::Event::MouseButtonReleased && Event.mouseButton.button == sf::Mouse::Right)
                pressed = false;
but is this the best way of doing it?

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: How to detect a single buttonpress?
« Reply #5 on: November 11, 2012, 01:36:48 am »
I had looked at the tutorials but none of that stuff helped me, because it still checks is the mouse is down every frame and therefor do something every frame.
This is not true. The statement
Event.type == sf::Event::MouseButtonPressed
will only return true if the state of the mouse button changes from not clicking to clicking.. If you hold it or release it, it will return false. You probably didn't wrap it in a a while loop to poll for events which is explained at the top of that tutorial.

Teras23

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to detect a single buttonpress?
« Reply #6 on: November 11, 2012, 12:52:08 pm »
So I was messing around with this and I think this is what you mean.
                sf::Event Event;
                while(App.pollEvent(Event))
                {
                        switch(Event.type)
                        {
                        case sf::Event::MouseButtonPressed:
                                        std::cout << "Mouse pressed";
                        }
                }
 
But I had problems with having this outside of the main function. I dont know if this is event supposed to be done outside the main function but when I had this in a function that was called every frame it did not work normally.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Re: How to detect a single buttonpress?
« Reply #7 on: November 11, 2012, 01:24:22 pm »
Come up with a meaningful problem description, if you want us to help you. Show a complete code representing the problem.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Teras23

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to detect a single buttonpress?
« Reply #8 on: November 11, 2012, 02:38:20 pm »
#include <SFML\Graphics.hpp>
void Mouse(sf::RenderWindow &App);
int _tmain(int argc, _TCHAR* argv[])
{      
        sf::RenderWindow App(sf::VideoMode(1280, 720),"Testing");

        while(App.isOpen())
        {
                sf::Event Event;
                while(App.pollEvent(Event))
                {
                        switch(Event.type)
                        {
                        case sf::Event::Closed:
                                App.close();
                        }
                }
                App.clear();
                Mouse(App);
                App.display();
        }
        return 0;
}

void Mouse(sf::RenderWindow &App)
{
        sf::Event Event;
        while(App.pollEvent(Event))
        {
                switch(Event.type)
                {
                case sf::Event::KeyPressed:
                        std::cout << "test";
                }
        }

}
 
It does not print out test every time you press a key it does it randomly or if you hold down the key it stats printing it out but not in a fixed pattern.

G.

  • Hero Member
  • *****
  • Posts: 1554
    • View Profile
Re: How to detect a single buttonpress?
« Reply #9 on: November 11, 2012, 03:52:55 pm »
2 event loops? Why? Don't. ;)

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: How to detect a single buttonpress?
« Reply #10 on: November 11, 2012, 04:03:30 pm »
Read the tutorial of events of 1.6. That should solve it. The problem exists because you use while (App.pollEvent(Event)) 2 times in one loop. The tutorial explains what it exactly does.

It is generally a bad idea to create events in other classes. Or you give the event through a reference or pointer, or you only process events in your main loop.

Teras23

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: How to detect a single buttonpress?
« Reply #11 on: November 11, 2012, 04:44:23 pm »
Oh... I was using sfml 2.0 so I only read the 2.0 tutorial and that does not have a lot in it, but thanks.

didii

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: How to detect a single buttonpress?
« Reply #12 on: November 11, 2012, 05:09:18 pm »
Yeah, I started with using 1.6 thus read all tutorials of 1.6. Then I switched to 2.0 and read all the tutorials again. That helped :)