SFML community forums

Help => Window => Topic started by: MickeyKnox on September 04, 2021, 06:45:03 pm

Title: sf::Mouse::isButtonPressed returns always false for XButtons
Post by: MickeyKnox 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.
Title: Re: sf::Mouse::isButtonPressed returns always false for XButtons
Post by: eXpl0it3r 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 (https://github.com/SFML/SFML/blob/master/src/SFML/Window/Unix/InputImpl.cpp#L211-212) 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 :)