SDL2 Event Handling

How can I handle multiple types of events in SDL2, such as keyboard and mouse events?

SDL2 provides an event system that allows you to handle various types of events, including keyboard and mouse events. Here's an example of how to handle multiple event types in an SDL2 application:

#include <SDL.h>

#include <iostream>

int main(int argc, char* argv[]) {
  SDL_Init(SDL_INIT_VIDEO);

  SDL_Window* window{SDL_CreateWindow(
    "Event Handling",
    SDL_WINDOWPOS_UNDEFINED,
    SDL_WINDOWPOS_UNDEFINED,
    800, 600, SDL_WINDOW_SHOWN
  )};

  SDL_Event event;
  bool quit = false;
  while (!quit) {
    while (SDL_PollEvent(&event)) {
      switch (event.type) {
        case SDL_QUIT:
          quit = true;
          break;
        case SDL_KEYDOWN:
          if (event.key.keysym.sym
            == SDLK_ESCAPE) {
            quit = true;
          } else {
            std::cout << "Key pressed: "
              << SDL_GetKeyName(
                event.key.keysym.sym) << '\n';
          }
          break;
        case SDL_MOUSEBUTTONDOWN:
          std::cout << "Mouse button pressed: "
            << event.button.button << "\n";
          break;
        case SDL_MOUSEMOTION:
          std::cout << "Mouse position: ("
            << event.motion.x << ", "
            << event.motion.y << ")\n";
          break;
      }
    }
  }

  SDL_DestroyWindow(window);
  SDL_Quit();

  return 0;
}

In this example, we use a switch statement inside the event loop to handle different event types. The event.type field determines the type of event that occurred.

  • For the SDL_QUIT event, we set the quit flag to true to exit the application.
  • For the SDL_KEYDOWN event, we check if the pressed key is the Escape key ( SDLK_ESCAPE) to quit the application. Otherwise, we print the name of the pressed key using SDL_GetKeyName.
  • For the SDL_MOUSEBUTTONDOWN event, we print the mouse button that was pressed using event.button.button.
  • For the SDL_MOUSEMOTION event, we print the current mouse position using event.motion.x and event.motion.y.

You can extend this event handling code to include additional event types and perform specific actions based on your application's requirements.

Remember to include the necessary headers and initialize SDL2 properly before handling events. Also, make sure to clean up resources, such as windows and renderers, before quitting the application.

Building SDL2 from Source (GCC and Make)

This guide walks you through the process of compiling SDL2, SDL_image, and SDL_ttf libraries from source

Questions & Answers

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

SDL2: Static vs Dynamic Linking
What is the difference between static and dynamic linking when building SDL2 from source?
Creating Multiple Windows with SDL2
How can I create multiple windows in an SDL2 application?
SDL2: Renderer vs Surface
What is the difference between an SDL_Renderer and an SDL_Surface in SDL2?
Creating a Fullscreen Window in SDL2
How can I create a fullscreen window in SDL2?
Audio Playback with SDL2
How can I play audio files using SDL2?
Or Ask your Own Question
Get an immediate answer to your specific question using our AI assistant