Handling Mouse Input

SDL Mouse Motion Events

What is the difference between SDL_MOUSEMOTION and SDL_MouseMotionEvent?

Abstract art representing computer programming

SDL_MOUSEMOTION and SDL_MouseMotionEvent are both related to mouse movement in SDL, but they serve different purposes in the event handling system.


SDL_MOUSEMOTION is an event type constant used to identify mouse motion events. When the mouse is moved, SDL generates an event with this type.

You check for this event type in your event loop to determine if the user has moved the mouse.

Here’s an example of how to use SDL_MOUSEMOTION in an event loop:

SDL_Event event;
while (SDL_PollEvent(&event)) {
  if (event.type == SDL_MOUSEMOTION) {
    std::cout << "Mouse moved to - x: "
      << event.motion.x << ", y: "
      << event.motion.y << '\n';  


SDL_MouseMotionEvent is a structure that contains detailed information about a mouse motion event.

When SDL_PollEvent() updates the SDL_Event structure with a mouse motion event, the motion member of the SDL_Event structure is an SDL_MouseMotionEvent.

Here’s an example structure:

typedef struct SDL_MouseMotionEvent {
  Uint32 type;      // Event type, SDL_MOUSEMOTION
  Uint32 timestamp; // Timestamp of the event
  Uint32 windowID;  // The window with mouse focus
  Uint32 which;     // The mouse instance ID
  Uint32 state;     // The current button state
  Sint32 x;         // X coordinate of the mouse
  Sint32 y;         // Y coordinate of the mouse
  Sint32 xrel;      // Relative motion in the X direction
  Sint32 yrel;      // Relative motion in the Y direction
} SDL_MouseMotionEvent;

Differences and Usage

  • Event Type: SDL_MOUSEMOTION is an event type constant, while SDL_MouseMotionEvent is a structure containing detailed information about the mouse motion event.
  • Detection: Use SDL_MOUSEMOTION to detect a mouse motion event in the event loop. Once detected, you can access the motion member of the SDL_Event structure to get details.
  • Details: SDL_MouseMotionEvent provides detailed information such as the cursor's new position (x, y), relative movement (xrel, yrel), and the state of mouse buttons during the movement.

Here’s a full example demonstrating how to use both:

#include <SDL.h>
#include <iostream>

int main(int argc, char* argv[]) {
  if (SDL_Init(SDL_INIT_VIDEO) != 0) {
    std::cerr << "SDL_Init Error: "
      << SDL_GetError() << '\n';
    return 1;

  SDL_Window* window = SDL_CreateWindow(
    "Mouse Motion",
    100, 100, 640, 480,

  if (window == nullptr) {
    std::cerr << "SDL_CreateWindow Error: "
      << SDL_GetError() << '\n';
    return 1;

  SDL_Event event;
  bool running = true;
  while (running) {
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      } else if (event.type == SDL_MOUSEMOTION) {
        SDL_MouseMotionEvent& motion = event.motion;

        std::cout << "Mouse moved to - x: "
          << motion.x << ", y: " << motion.y << '\n';  

        std::cout << "Relative movement - xrel: "
          << motion.xrel << ", yrel: "
          << motion.yrel << '\n';  

  return 0;
Mouse moved to - x: 150, y: 200
Relative movement - xrel: 5, yrel: 5

In summary, use SDL_MOUSEMOTION to detect mouse movement events and SDL_MouseMotionEvent to access detailed information about the motion.

This Question is from the Lesson:

Handling Mouse Input

Learn how to detect and handle mouse input events in SDL, including mouse motion, button clicks, and window entry/exit.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Handling Mouse Input

Learn how to detect and handle mouse input events in SDL, including mouse motion, button clicks, and window entry/exit.

Part of the course:

Game Dev with SDL2

Learn C++ and SDL development by creating hands on, practical projects inspired by classic retro games

Free, unlimited access

This course includes:

  • 36 Lessons
  • 100+ Code Samples
  • 91% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved