Getting window under mouse in SDL2

Is there a way to get window under current mouse position? As far as i tested event poll isnt terribly reliable way at least under windows 10. Entering/leaving doesnt work reliably when moving real quick or when dragging (especially last event is tricky). So i decided to make workaround.

                    SDL.SDL_GetGlobalMouseState(out int x, out int y);
                    foreach (var win in windows)
                        var pos = win.Position;
                        var size = win.Size;
                        var xRel = x - pos.x;
                        var yRel = y - pos.y;
                        if (xRel > 0 && yRel > 0 && xRel < size.width && yRel < size.height)
                            InputHandler.winUnderMouse = win.windowId;
                            if (win.handle == SDL.SDL_GetMouseFocus())


Im using C# binding to SDL2. As you see every mouse move i test if ANY window is under mouse. Additionally, if foreach meet focused window that lie under mouse then immediately stop since focused window is practically guaranteed to be on top.

The problem is that this implementation is naive. It doesnt check “depth order” in case of overlapping windows.

Is there a way or workaround/hack to do so? via hit test or something else?

If it isnt supported, then consider it as feature request.

thanks for reading.

Out of curiosity, if you have a moment and can answer the following: what user-facing use case(s) are there for determining the ‘depth order’ of different SDL window(s)?

In 1st post there is one use -case which i encountered where depth order is useful: getting window under mouse (worst scenario: at any time without polling events, using GetGlobalMouseState, with multiple windows overlapping each other, which is currently literally impossible). But thats pretty much all so far (note: it might not be all real-world use-cases, just that i encountered this one use-case).

If u dont want to expose depth then all i need is one method which take point (x,y) and return window handle.

BTW: I did more testing and it turns out that offender is actually SDL_CaptureMouse enabled/disabled every mouse up/down not event list itself. When SDL_CaptureMouse is enabled and i start dragging i receive events from both windows: where drag started and where mouse over is. In order to fix it i had to move enabling/disabling to enter/leave window events. I wonder if its expected or actually a bug in CaptureMouse.

I wonder if its expected or actually a bug in CaptureMouse.

This sounds like a bug in SDL_CaptureMouse.

Use the windowID in the event structure and pass it to SDL_GetWindowFromID(). That gives you the pointer (C++, object ref in C#?) to the window structure.

Note: SDL_SetWindowData() and SDL_GetWindowData() are very useful if you wrap SDL window object inside your own classed. For example, store a pointer/ref to your wrapper class object, and retrieve it via the pointer return by SDL_GetWindowFromID().

Hope this helps.