SDL Event which Field

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

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.

Mouse Input Basics

Discover how to process mouse input, including position tracking and button presses

Questions & Answers

Answers are generated by AI models and may not have been reviewed. Be mindful when running any code on your device.

SDL Coordinate System: Why Y-Down?
Why does SDL use a y-down coordinate system instead of y-up?
SDL: Get Mouse Screen Coordinates
Can I get mouse coordinates relative to the screen instead of the window?
SDL_PollEvent() vs SDL_WaitEvent()
What's the difference between SDL_PollEvent() and SDL_WaitEvent()?
SDL Event Timestamp Field
What does the timestamp field in the event structures mean?
SDL: Custom Mouse Cursor Appearance
Can I change the mouse cursor's appearance (e.g., to a crosshair)?
SDL Double Click Timing Customization
How does SDL determine the time window for registering a double click? Can I customize this timing?
Retrieve Mouse Position
How do I retrieve the current position of the mouse cursor in SDL?
SDL Mouse Motion Events
What is the difference between SDL_MOUSEMOTION and SDL_MouseMotionEvent?
Detect Double Clicks in SDL
How can I detect double clicks using SDL?
SDL_MouseButtonEvent Structure
What is the SDL_MouseButtonEvent structure used for?
Calculate Mouse Position
How can I calculate the distance of the mouse cursor from the edges of the window?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant