Mouse Capture and Global Mouse State

Selective Mouse Button Auto-Capture

Is there a way to disable auto-capture for specific mouse buttons but keep it for others?

Abstract art representing computer programming

SDL's auto-capture feature is controlled through the SDL_HINT_MOUSE_AUTO_CAPTURE hint, but it doesn't provide direct per-button control. However, we can implement our own selective auto-capture system by disabling the built-in auto-capture and managing it ourselves.

Here's a complete implementation that demonstrates this approach:

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

class SelectiveMouseCapture {
public:
  SelectiveMouseCapture() {
    // Disable SDL's built-in auto-capture
    SDL_SetHint(
      SDL_HINT_MOUSE_AUTO_CAPTURE, "0"); 
  }

  void
    EnableAutoCaptureForButton(Uint8 button) {
    if (button < MaxButtons) {
      autoCaptureButtons.set(button);
    }
  }

  void
    DisableAutoCaptureForButton(Uint8 button) {
    if (button < MaxButtons) {
      autoCaptureButtons.reset(button);
    }
  }

  void HandleMouseDown(
    SDL_MouseButtonEvent& event) {
    if (event.button < MaxButtons &&
      autoCaptureButtons.test(event.button)) {
      SDL_CaptureMouse(SDL_TRUE);
      isCapturing = true;
    }
  }

  void HandleMouseUp(
    SDL_MouseButtonEvent& event) {
    if (isCapturing && event.button < MaxButtons
      &&
      autoCaptureButtons.test(event.button)) {
      SDL_CaptureMouse(SDL_FALSE);
      isCapturing = false;
    }
  }

private:
  static const size_t MaxButtons = 32;
  std::bitset<MaxButtons> autoCaptureButtons;
  bool isCapturing = false;
};

int main(int argc, char** argv) {
  SDL_Init(SDL_INIT_VIDEO);
  SDL_Window* window =
    SDL_CreateWindow("Selective Capture",
                     SDL_WINDOWPOS_CENTERED,
                     SDL_WINDOWPOS_CENTERED,
                     800, 600,
                     SDL_WINDOW_SHOWN);

  SelectiveMouseCapture captureManager;

  // Enable auto-capture for left button only
  captureManager.EnableAutoCaptureForButton(
    SDL_BUTTON_LEFT); 

  bool running = true;
  while (running) {
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
      if (event.type == SDL_QUIT) {
        running = false;
      } else if (event.type ==
        SDL_MOUSEBUTTONDOWN) {
        captureManager.HandleMouseDown(
          event.button);
        std::cout << "Button "
          << (int)event.button.button
          << " pressed\n";
      } else if (event.type == SDL_MOUSEBUTTONUP) {
        captureManager.HandleMouseUp(
          event.button);
        std::cout << "Button "
          << (int)event.button.button
          << " released\n";
      }
    }

    // Show capture state
    bool isCaptured = SDL_GetWindowFlags(window)
      & SDL_WINDOW_MOUSE_CAPTURE;
    std::cout << "Capture State: " << (
        isCaptured ? "Active" : "Inactive")
      << "\r" << std::flush;
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}

You can configure which buttons should trigger auto-capture:

SelectiveMouseCapture captureManager;

// Example configurations:
// Only left button auto-captures
captureManager.EnableAutoCaptureForButton(
  SDL_BUTTON_LEFT);

// Left and right buttons auto-capture
captureManager.EnableAutoCaptureForButton(
  SDL_BUTTON_LEFT);
captureManager.EnableAutoCaptureForButton(
  SDL_BUTTON_RIGHT);

// Disable auto-capture for middle button
captureManager.DisableAutoCaptureForButton(
  SDL_BUTTON_MIDDLE);

This implementation offers several advantages:

  • Fine-grained control over which buttons trigger auto-capture
  • Clean separation of capture management logic
  • Easy to modify or extend the behavior
  • Maintains proper capture state tracking

Key considerations when using selective auto-capture:

  • Remember to handle focus loss appropriately
  • Consider adding visual feedback for capture state
  • Be mindful of button combinations
  • Test thoroughly with different input scenarios
This Question is from the Lesson:

Mouse Capture and Global Mouse State

Learn how to track mouse movements and button states across your entire application, even when the mouse leaves your window.

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

This Question is from the Lesson:

Mouse Capture and Global Mouse State

Learn how to track mouse movements and button states across your entire application, even when the mouse leaves your window.

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

Free, unlimited access

This course includes:

  • 75 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 © 2025 - All Rights Reserved