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

Author Topic: How to check if both mouse button is pressed  (Read 3355 times)

0 Members and 1 Guest are viewing this topic.

ifinox

  • Newbie
  • *
  • Posts: 9
    • View Profile
How to check if both mouse button is pressed
« on: February 17, 2017, 12:58:15 pm »
Hello. I need to check if both lmb and rmb is pressed. How can i do that?

if (event.type == sf::Event::MouseButtonPressed)
{
        if (event.mouseButton.button == sf::Mouse::Left && event.mouseButton.button == sf::Mouse::Right)
                std::cout << "LMB+RMB\n";
        else if (event.mouseButton.button == sf::Mouse::Left)
                std::cout << "LMB\n";
        else if (event.mouseButton.button == sf::Mouse::Right)
                std::cout << "RMB\n";
}
 

That code I have now and it is obvious that not work, but how to do that properly?

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: How to check if both mouse button is pressed
« Reply #1 on: February 17, 2017, 01:16:51 pm »
Store the state of your mouse buttons, e.g. in two boolean values. Then just compare both of them later on.

ifinox

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: How to check if both mouse button is pressed
« Reply #2 on: February 17, 2017, 04:34:50 pm »
Thank you!  :)
So if anyone will have similar problem here's the code:
if (event.type == sf::Event::MouseButtonPressed)
{
        bool left, right;
        left = right = false;
        if (mouse.isButtonPressed(sf::Mouse::Left))
                left = true;
                                       
        if (mouse.isButtonPressed(sf::Mouse::Right))
                right = true;

        if (left && right)
                std::cout << "LMB+RMB\n";
        else if (left)
                std::cout << "LMB\n";
        else if (right)
                std::cout << "RMB\n";
}
 

 

anything