Handling Mouse Input

Detect Double Clicks in SDL

How can I detect double clicks using SDL?

Abstract art representing computer programming

Detecting double clicks in SDL involves checking the clicks member of the SDL_MouseButtonEvent structure.

This member indicates the number of times the mouse button was clicked in quick succession. A value of 2 in the clicks member signifies a double click.

Here’s a basic example of detecting double clicks:

#include <SDL.h>

#include <iostream>

void HandleMouseButton(
  const SDL_MouseButtonEvent& buttonEvent) {
  if (buttonEvent.clicks == 2) {
    std::cout << "Double Click Detected\n";  
  }
}

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(
    "Double Click Detection",
    100, 100, 640, 480,
    SDL_WINDOW_SHOWN
  );

  if (window == nullptr) {
    std::cerr << "SDL_CreateWindow Error: "
      << SDL_GetError() << '\n';
    SDL_Quit();
    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_MOUSEBUTTONDOWN) {
        HandleMouseButton(event.button);
      }
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();
  return 0;
}
Double Click Detected

How It Works

  1. Event Loop: The main event loop calls SDL_PollEvent() to retrieve events.
  2. Mouse Button Down: When a SDL_MOUSEBUTTONDOWN event is detected, it calls the HandleMouseButton() function.
  3. Double Click Detection: Inside HandleMouseButton(), the clicks member of the SDL_MouseButtonEvent structure is checked. If clicks == 2, a double click is detected.

Points to Note

  • Timing: The detection of double clicks relies on the system’s double-click time setting. SDL respects the user’s operating system settings for double-click speed, making it more reliable than implementing custom timing logic.
  • State: The clicks member is present in both SDL_MOUSEBUTTONDOWN and SDL_MOUSEBUTTONUP events. Ensure you handle the event you care about.

Example Enhancement

You might also want to distinguish between single and double clicks or handle specific mouse buttons:

void HandleMouseButton(
  const SDL_MouseButtonEvent& buttonEvent) {
  if (buttonEvent.clicks == 2) {
    if (buttonEvent.button == SDL_BUTTON_LEFT) {
      std::cout << "Left Button Double Click\n";  
    }
  } else {
    if (buttonEvent.button == SDL_BUTTON_LEFT) {
      std::cout << "Left Button Single Click\n";  
    }
  }
}
Left Button Double Click
Left Button Single Click

This code checks which button was clicked and whether it was a single or double click. Using SDL’s built-in double-click detection is preferred over custom logic because it respects user settings and provides accurate results.

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.

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:

  • 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