#include#include int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Mouse Event Example"); while (window.isOpen()) { sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left) { std::cout << "Left mouse button was pressed" << std::endl; std::cout << "Button code: " << event.mouseButton.button << std::endl; } else if (event.type == sf::Event::MouseButtonReleased && event.mouseButton.button == sf::Mouse::Left) { std::cout << "Left mouse button was released" << std::endl; std::cout << "Button code: " << event.mouseButton.button << std::endl; } } window.clear(); window.display(); } return 0; }
#includeIn this example, the getButton method is used in conjunction with the isButtonPressed method to create a custom mouse cursor that changes color when the left mouse button is pressed. The package library used in this example is also the SFML graphics library.#include int main() { sf::RenderWindow window(sf::VideoMode(800, 600), "Custom Mouse Cursor Example"); sf::CircleShape cursor(10); cursor.setFillColor(sf::Color::Red); while (window.isOpen()) { sf::Vector2i MousePosition = sf::Mouse::getPosition(window); cursor.setPosition(static_cast (MousePosition.x), static_cast (MousePosition.y)); sf::Event event; while (window.pollEvent(event)) { if (event.type == sf::Event::Closed) { window.close(); } } int button = sf::Mouse::isButtonPressed(sf::Mouse::Left); if (button) { cursor.setFillColor(sf::Color::Green); } else { cursor.setFillColor(sf::Color::Red); } window.clear(); window.draw(cursor); window.display(); } return 0; }