SDL icon indicating copy to clipboard operation
SDL copied to clipboard

Allow confining a hardare cursor to the window rect automatically

Open ZivDero opened this issue 1 month ago • 4 comments

As far as I can see, SDL_SetWindowMouseGrab only works with a software cursor, and a hardware cursor is unaffected by this. The mouse can still be restricted manually in Windows using ClipCursor. However, it would be nice if there was an automatic and platform-agnostic way of doing this.

ZivDero avatar Oct 28 '25 12:10 ZivDero

You mean something like SDL_SetWindowMouseRect?

slouken avatar Oct 28 '25 13:10 slouken

You mean something like SDL_SetWindowMouseRect?

In theory, yes. However, it does not seem to work for me?

    SDL_Rect rect;
    SDL_GetWindowPosition(SDLWindow, &rect.x, &rect.y);
    SDL_GetWindowSize(SDLWindow, &rect.w, &rect.h);
    SDL_SetWindowMouseRect(SDLWindow, &rect);

is ineffective (the rect appears to be correct, my screen size at (0,0) for a fullscreen window).

    RECT client_rect;
    GetClientRect(MainWindow, &client_rect);
    POINT ul = {client_rect.left, client_rect.top};
    POINT lr = {client_rect.right, client_rect.bottom};
    MapWindowPoints(MainWindow, nullptr, &ul, 1);
    MapWindowPoints(MainWindow, nullptr, &lr, 1);
    RECT clip_rect = {ul.x, ul.y, lr.x, lr.y};
    ClipCursor(&clip_rect);

works as desired.

ZivDero avatar Oct 29 '25 05:10 ZivDero

The rectangle is relative to the client area, so x and y would be 0,0. Although mouse grab should also work. Can you run the SDL testsprite test program and press Ctrl-G and see if that works?

slouken avatar Oct 29 '25 06:10 slouken

The rectangle is relative to the client area, so x and y would be 0,0. Although mouse grab should also work. Can you run the SDL testsprite test program and press Ctrl-G and see if that works?

That appears to work, including even with a window that's over 2 monitors with different DPI settings. Intriguing.

ZivDero avatar Oct 29 '25 06:10 ZivDero