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

Author Topic: sf::Mouse::isButtonPressed returns always false for XButtons  (Read 7071 times)

0 Members and 1 Guest are viewing this topic.

MickeyKnox

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
sf::Mouse::isButtonPressed returns always false for XButtons
« on: September 04, 2021, 06:45:03 pm »
Consider this minimal example to reproduce the problem:

int main()
{
    sf::Window window(sf::VideoMode(800, 600), "test");

    sf::Event event;

    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            switch (event.type)
            {
                case sf::Event::Closed:
                    window.close();
                    break;

                case sf::Event::MouseButtonPressed:
                    switch (event.mouseButton.button)
                    {
                        case sf::Mouse::XButton1:
                        case sf::Mouse::XButton2:
                        case sf::Mouse::Left:
                            if (sf::Mouse::isButtonPressed(sf::Mouse::XButton1))
                                std::cout << "XButton1 pressed" << std::endl;
                            else if (sf::Mouse::isButtonPressed(sf::Mouse::XButton2))
                                std::cout << "XButton2 pressed" << std::endl;
                            else if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
                                std::cout << "Left button pressed" << std::endl;
                            else
                                std::cout << "No button pressed" << std::endl;
                            break;
                        default:
                            break;
                    }
                    break;
            }
        }
    }
}
 

The event for the XButtons is fired, yet sf::Mouse::isButtonPressed returns false; the output is "No button pressed". The problem doesn't occur for the left mouse button, the output is as expected "Left button pressed".

Have I found a bug? Or is there something funny with my mouse? Tested on Ubuntu 20.04.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: sf::Mouse::isButtonPressed returns always false for XButtons
« Reply #1 on: September 06, 2021, 02:43:50 pm »
Events and sf::Mouse::isButtonPressed use two different OS APIs, so there's always the chance that they don't respond in the same way, which is among other things also a reason to never mix the two.

Looking at the implementation it seems XButton1 and XButton2 are not supported by X11 in that way:
        case Mouse::XButton1: return false; // not supported by X
        case Mouse::XButton2: return false; // not supported by X
 

If you figure out a way to get it to work with X11, then that would be a great addition to SFML :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/