简体   繁体   中英

How do you set the cursor position in SDL2?

I want to be able to change the position of the cursor over my SDL2 window. I can't find anything in the documentation. Is there a way to do it?

Use the SDL_WarpMouseInWindow() function, it sets the mouse position in the SDL Window.

Demonstration:

auto* WIN = SDL_CreateWindow("text", 0, 0, Width, Height, SDL_WINDOW_RESIZABLE);
SDL_WarpMouseInWindow(WIN, 50, 50);/*sets mouse position to 50, 50 relative to the window */

Keep in mind, this invokes the SDL_MOUSEMOTION event so, if you're polling events, it will consider the change in mouse position as a mouse movement.

Here's a reference: https://wiki.libsdl.org/SDL_WarpMouseInWindow

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM