SFML community forums

Help => System => Topic started by: Romsik788 on June 12, 2020, 02:57:25 pm

Title: Problem with MouseButtonPressed
Post by: Romsik788 on June 12, 2020, 02:57:25 pm
I have code

if (event.type == Event::MouseButtonPressed)
        {
            if (event.key.code == Mouse::Left)
            {
                  //Some code
            }
        }

I need that when long hold down the left mouse button this code does not work. (When you click on the mouse, this code should work once, while further holding mouse down, the code should not work) How to do it?
P.S. Sorry my english  ;)
Title: Re: Problem with MouseButtonPressed
Post by: Arcade on June 12, 2020, 03:39:49 pm
Your code is incorrect. Instead of this:
if (event.key.code == Mouse::Left)

You should do this:
if (event.mouseButton.button == Mouse::Left)

event.key.code is for keyboard events.
Title: Re: Problem with MouseButtonPressed
Post by: Romsik788 on June 12, 2020, 04:33:41 pm
Fixed, but this still did not solve my problem
Title: Re: Problem with MouseButtonPressed
Post by: Romsik788 on June 12, 2020, 08:23:06 pm
I fixed the problem, it was necessary to put this code in the event loop. As a result, I have this:

Event event;
while(window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                {
                    window.close();
                }
            if (event.type == Event::MouseButtonPressed)
                {
                    if (event.mouseButton.button == Mouse::Left)
                        {
                            //Some code
                        }
                }
         }