Mouse Events

Learn how to handle mouse events with our own callbacks

Source Code

SFML Mouse Events

SFML captures six (6) types of Events when playing with your mouse

  • sf::Event::MouseMoved : Emitted when you move the mouse or play with the touchpad for portable computer
  • sf::Event::MouseButtonPressed : Emitted when a button is pressed
  • sf::Event::MouseButtonReleased : Emitted when a button is released
  • sf::Event::MouseWheelScrolled : Emitted when you scroll the mouse wheel up or down
  • sf::Event::MouseEntered : Emitted when the mouse enters the window surface
  • sf::Event::MouseLeft : Emitted when the mouse leaves the window surface
The code below shows how to handle those six (6) types of Events. The events MouseEntered and MouseLeft do not query any information, the other events all query at least the mouse position at the time of the Event. The events ButtonPressed and ButtonReleased show you the button pressed or released, SFML support up to five (5) buttons : Left, Right, Middle, and two extra buttons called XButton1 and XButton2. The event WheelScrolled informs you which wheel has been scrolled Vertical or Horizontal (some mice have two wheels !?) and how much the wheel has been scrolled (delta).
void handleEvent(const sf::Event& event)
{
    if (event.type == sf::Event::MouseMoved)
    {
        int position_x = event.mouseMove.x;
        int position_y = event.mouseMove.y;
    }
    if (event.type == sf::Event::MouseButtonPressed)
    {
        sf::Mouse::Button button    = event.mouseButton.button; //Left, Right, Middle, XButton1, XButton2
        int position_x              = event.mouseButton.x;
        int position_y              = event.mouseButton.y;
    }
    if (event.type == sf::Event::MouseButtonReleased)
    {
        sf::Mouse::Button button    = event.mouseButton.button; //Left, Right, Middle, XButton1, XButton2
        int position_x              = event.mouseButton.x;
        int position_y              = event.mouseButton.y;
    }
    if (event.type == sf::Event::MouseWheelScrolled)
    {
        sf::Mouse::Wheel wheel  = event.mouseWheelScroll.wheel; //sf::Mouse::VerticalWheel or sf::Mouse::HorizontalWheel
        float delta             = event.mouseWheelScroll.delta;
        int position_x          = event.mouseWheelScroll.x;
        int position_y          = event.mouseWheelScroll.y;
    }
    if (event.type == sf::Event::MouseEntered)
    {
        //do something
    }
    if (event.type == sf::Event::MouseLeft)
    {
        //do something
    }
}
						

Mouse Events Callbacks

Now let’s improve our code by creating our own callbacks. From now on will be using the four callbacks below :

  • void onMouseMoved(const sf::Vector2f& position)
  • void onMouseButton(const sf::Mouse::Button& button, const &isPressed, const sf::Vector2f& position)
  • void onMouseWheel(const sf::Mouse::Wheel& wheel, const float& delta, const sf::Vector2f& position)
  • void onMouseWindowSurface(const bool& mouseEntered)
void onMouseMoved(const sf::Vector2f& position)
{
    //do something
        //position.x
        //position.y
}
void onMouseButton(const sf::Mouse::Button& button, const &isPressed, const sf::Vector2f& position)
{
    //do something
        //button = sf::Mouse::Left, sf::Mouse::Right, sf::Mouse::Middle, sf::Mouse::XButton1, sf::Mouse::XButton2
        //isPressed = True or False
        //position.x
        //position.y
}
void onMouseWheel(const sf::Mouse::Wheel& wheel, const float& delta, const sf::Vector2f& position)
{
    //do something
        //wheel = sf::Mouse::VerticalWheel, sf::Mouse::HorizontalWheel
        //delta =   positive when moving up(toward the mouse front of the mouse),
        //          negative when moving down(toward the back of the mouse)
}
void onMouseWindowSurface(const bool& mouseEntered)
{
    //do something
        //mouseEntered = True (mouse entered), False (mouse left)
}

Now our handleEvent method looks like this

void handleEvent(const sf::Event& event)
{
    if (event.type == sf::Event::MouseMoved)
    {
        //callback
        onMouseMoved(sf::Vector2f(event.mouseMove.x, event.mouseMove.y));
    }
    if (event.type == sf::Event::MouseButtonPressed)
    {
        //callback
        onMouseButton(event.mouseButton.button, true, sf::Vector2f(event.mouseButton.x, event.mouseButton.y));
    }
    if (event.type == sf::Event::MouseButtonReleased)
    {
        //callback
        onMouseButton(event.mouseButton.button, false, sf::Vector2f(event.mouseButton.x, event.mouseButton.y));
    }
    if (event.type == sf::Event::MouseWheelScrolled)
    {
        //callback
        onMouseWheel(event.mouseWheelScroll.wheel, event.mouseWheelScroll.delta,sf::Vector2f(event.mouseWheelScroll.x, event.mouseWheelScroll.y));
    }
    if (event.type == sf::Event::MouseEntered)
    {
        onMouseWindowSurface(true);
    }
    if (event.type == sf::Event::MouseLeft)
    {
        onMouseWindowSurface(false);
    }
}