SDL: Get Mouse Screen Coordinates

Can I get mouse coordinates relative to the screen instead of the window?

Yes, SDL provides functions to get the mouse cursor's position relative to the global screen/desktop coordinates, rather than just relative to a specific application window.

Using SDL_GetGlobalMouseState()

The primary function for this is SDL_GetGlobalMouseState(). It retrieves the current state of the mouse, including its position on the screen.

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

int main(int argc, char** argv) {
  if (SDL_Init(SDL_INIT_VIDEO) < 0) {
    std::cerr << "SDL could not initialize! SDL_Error: "
      << SDL_GetError() << '\n';
    return 1;
  }

  // No window creation needed just to get global state

  int mouseX, mouseY;
  Uint32 mouseButtonState = SDL_GetGlobalMouseState(
    &mouseX, &mouseY                        
  );                                        

  std::cout << "Global Mouse Position - x: " << mouseX
            << ", y: " << mouseY << '\n';

  if (mouseButtonState & SDL_BUTTON(SDL_BUTTON_LEFT)) {
    std::cout << "Left mouse button is pressed globally.\n";
  }

  SDL_Quit();
  return 0;
}
Global Mouse Position - x: 1024, y: 512
Left mouse button is pressed globally.

This function takes pointers to two integers (int* x, int* y) which it fills with the current global x and y coordinates of the mouse cursor. It also returns a bitmask representing the state of the mouse buttons globally (useful if you need to know button states without relying on window focus).

SDL_GetMouseState() (Relative to Focused Window)

For comparison, remember that SDL_GetMouseState() provides the mouse position relative to the window that currently has mouse focus. If no window in your application has focus, the coordinates returned by SDL_GetMouseState() might not be meaningful or could be relative to the desktop.

// Inside your main loop or where you have a window context
int windowMouseX, windowMouseY;
SDL_GetMouseState(&windowMouseX, &windowMouseY);
// windowMouseX, windowMouseY are relative to the focused window

When to Use Global Coordinates?

Getting global coordinates is less common in game development than window-relative coordinates, but it can be useful for:

  • Applications that interact with the entire desktop (e.g., screen capture tools).
  • Positioning new windows based on the current mouse position.
  • Debugging purposes.

For most in-game interactions (clicking buttons, selecting units, aiming), you'll typically use the window-relative coordinates provided by mouse events (event.motion.x, event.button.x) or SDL_GetMouseState().

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_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 Event which Field
What does the which field in the event structures refer to (e.g., multiple mice)?
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