Search code examples
c++sdlmouse

How to detect what mouse key is pressed in C++ SDL?


How do I determine which exact mouse button (right, left, middle, etc.) is pressed? I'm using SDL. Here is the code that I have, which says if any mouse button is clicked or not:

            case SDL_MOUSEBUTTONDOWN:
              {
                Mouse_Pressed = event.button.clicks;
              } break;

            case SDL_MOUSEBUTTONUP:
              {
                Mouse_Pressed = 0;
              } break;

Solution

  • You can access that through the event button field.

    case SDL_MOUSEBUTTONUP:
                switch ( ev.button.button ) {
                    case SDL_BUTTON_LEFT:
                        break;
                    case SDL_BUTTON_RIGHT:
                        break;
                    case SDL_BUTTON_MIDDLE:
                        break;
                    case SDL_BUTTON_X1:
                        break;
                    case SDL_BUTTON_X2:
                        break;
                }
    

    https://wiki.libsdl.org/SDL_MouseButtonEvent