sfml mouse click event code example

Example 1: sfml mouse click

if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
    // left click...
}
// get global mouse position
sf::Vector2i position = sf::Mouse::getPosition();
// set mouse position relative to a window
sf::Mouse::setPosition(sf::Vector2i(100, 200), window);

Example 2: use envent in csfml

sf::Event event;

// while there are pending events...
while (window.pollEvent(event))
{
    // check the type of the event...
    switch (event.type)
    {
        // window closed
        case sf::Event::Closed:
            window.close();
            break;

        // key pressed
        case sf::Event::KeyPressed:
            ...
            break;

        // we don't process other types of events
        default:
            break;
    }
}

Tags:

Cpp Example