SFML community forums

Help => Graphics => Topic started by: AdventWolf on September 04, 2010, 09:33:46 pm

Title: Registering Single Mouse Click
Post by: AdventWolf on September 04, 2010, 09:33:46 pm
Hello, I've lurked here for a while to help solve some problems here and there and I have searched for the answer to the problem I have now, but I haven't found much solid info.

I am trying to use the mouse to select buttons in a program, but holding the mouse down for longer than half a second will output the button multiple times.

How can I fix it so that the button won't be initiated until I release the mouse click on the button?

Thanks.
Title: Registering Single Mouse Click
Post by: Xorlium on September 04, 2010, 09:44:14 pm
Code: [Select]
YourWindow.EnableKeyRepeat(false);
Title: Registering Single Mouse Click
Post by: AdventWolf on September 04, 2010, 10:19:39 pm
I guess I must be doing something wrong, I have tried that, but I still get infinite output if the mouse is held down.

What is wrong with this sample?

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
sf::RenderWindow App(sf::VideoMode(600,600),"Test");
App.EnableKeyRepeat(false);

    while( App.IsOpened() )
    {
const sf::Input& Input = App.GetInput();
App.GetInput().GetMouseX(), App.GetInput().GetMouseY();
sf::Event Event;
while( App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
}
if( Input.IsMouseButtonDown(sf::Mouse::Left) )
std::cout << "left" << std::endl;
App.Display();
}
std::cout << "DONE" << std::endl;
}
Title: Registering Single Mouse Click
Post by: Lupinius on September 04, 2010, 10:56:44 pm
You're checking if the mouse mouse button is pressed every frame. So as long as you press the mouse button this will return true.
Title: Registering Single Mouse Click
Post by: AdventWolf on September 04, 2010, 11:05:55 pm
Ah ok thanks I got it to work! So for other readers that have had this problem, here's the code:

Code: [Select]
while( App.GetEvent(Event))
{
if (Event.Type == sf::Event::Closed)
App.Close();
else if( Event.Type == Event.MouseButtonReleased && Event.MouseButton.Button == sf::Mouse::Left )
std::cout << "LEFT" << std::endl;
}
Title: Registering Single Mouse Click
Post by: nalpam on October 17, 2011, 02:24:39 pm
Thanks for providing the answer to the situation. I was asking myself how to achieve the same effect.

:wink: