Mouse Input Basics

SDL Event which Field

What does the which field in the event structures refer to (e.g., multiple mice)?

Abstract art representing computer programming

The which field, found in input-related event structures like SDL_MouseMotionEvent, SDL_MouseButtonEvent, SDL_MouseWheelEvent, SDL_KeyboardEvent, and joystick/controller events, serves as an identifier for the specific input device instance that generated the event.

Purpose: Handling Multiple Devices

Its primary purpose is to distinguish between input events when multiple devices of the same type are connected to the system.

  • Multiple Mice/Keyboards: If a user has more than one mouse or keyboard plugged in (less common for desktops, but possible), the which field allows your application to identify which specific mouse moved or which specific keyboard generated a key press. Each connected device will typically have a unique instance ID assigned by SDL.
  • Multiple Joysticks/Game Controllers: This is a very common use case. Most systems support multiple game controllers. The which field (often referred to as the instance ID in joystick/controller APIs) is essential for routing input from Player 1's controller to Player 1's character, Player 2's input to Player 2, and so on.

Example Scenario (Conceptual)

Here’s an example event loop that applies these concepts:

SDL_Event event;
while (SDL_PollEvent(&event)) {
  if (event.type == SDL_MOUSEBUTTONDOWN) {
    std::cout << "Mouse button down event from device ID: "
              << event.button.which << '\n'; 

    if (event.button.which == 0) {
      // Handle input from the first mouse
    } else if (event.button.which == 1) {
      // Handle input from the second mouse (if present)
    }
  } else if (event.type == SDL_JOYBUTTONDOWN) {
    std::cout << "Joystick button down from instance ID: "
              << event.jbutton.which << '\n'; 
    // Route input based on joystick instance ID
    RouteJoystickInput(event.jbutton.which, event.jbutton.button);
  }
}

Special Value: SDL_TOUCH_MOUSEID

There's a special predefined value, SDL_TOUCH_MOUSEID. If the which field in a mouse event structure equals this value, it indicates that the mouse event was synthesized from a touch event.

This happens on touch-enabled devices where SDL emulates mouse behavior based on touch input (e.g., a tap generating SDL_MOUSEBUTTONDOWN and SDL_MOUSEBUTTONUP). You can use this to differentiate between actual hardware mouse events and touch-emulated ones if necessary.

if (event.type == SDL_MOUSEBUTTONDOWN) {
  if (event.button.which == SDL_TOUCH_MOUSEID) {
    std::cout << "This click came from a touch event.\n";
  } else {
    std::cout << "This click came from a hardware mouse (ID: "
              << event.button.which << ").\n";
  }
}

When Is It Important?

For typical single-player desktop applications with one mouse and keyboard, you might often ignore the which field for mouse/keyboard events (implicitly handling input from the default device, usually ID 0).

However, it becomes crucial when developing applications that explicitly support multiple input devices of the same type, especially game controllers.

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

sdl2-promo.jpg
Part of the course:

Game Dev with SDL2

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

This course includes:

  • 110 Lessons
  • 92% Positive Reviews
  • Regularly Updated
  • Help and FAQs
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 © 2025 - All Rights Reserved