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

Author Topic: Input problem?  (Read 2140 times)

0 Members and 1 Guest are viewing this topic.

Mr_Gnome

  • Newbie
  • *
  • Posts: 6
    • View Profile
Input problem?
« on: May 02, 2011, 08:21:13 am »
I'm having sort of a problem with how I'm handling my mouse click events. I'm using input and I have it so that when I click the window it will call a function, but it will call said function several time when I just clicked once.

With this code here:
Code: [Select]
#include <SFML/Window.hpp>
#include <iostream>

void Hello() {
std::cout << "hello" << "\n";
}

int main()
{
    sf::Window App(sf::VideoMode(800, 600, 32), "Input?");

    const sf::Input& Input = App.GetInput();

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();

            if ((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
                App.Close();
        }
        bool LeftButtonDown = Input.IsMouseButtonDown(sf::Mouse::Left);
        if(LeftButtonDown) Hello();

        App.Display();
    }

    return EXIT_SUCCESS;
}

I get:
Quote
hello
hello
hello
hello
hello
hello
hello
hello
hello
[...]*

*you get the point.

Is it that I'm handing my mouse clicks wrong or what?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Input problem?
« Reply #1 on: May 02, 2011, 08:39:35 am »
Input.IsMouseButtonDown returns true continuously as long as the mouse button is pressed, so yes it is perfectly normal that your function gets called many times.

If you want to be notified once, when the click happens, use events instead (see sf::Event::MouseButtonPressed).
Laurent Gomila - SFML developer

Mr_Gnome

  • Newbie
  • *
  • Posts: 6
    • View Profile
Input problem?
« Reply #2 on: May 02, 2011, 08:58:43 am »
Ok, thanks... I got what I wanted to work :)

CJ_COIMBRA

  • Full Member
  • ***
  • Posts: 112
    • ICQ Messenger - 13077864
    • View Profile
    • http://www.cjcoimbra.com
    • Email
Input problem?
« Reply #3 on: July 01, 2011, 07:09:20 pm »
Strangely I am getting the same problem and I am using Event.

EDIT: Sorry, my bad. Problem solved!
CJ