Limit mouse cursor to screen borders in fullscreen mode

Hello! I’m using a custom sprite as a mouse cursor and so far I am able to limit sprite’s position on screen. The cursor itself though can be moved outside of screen and you have to move it back into the screen borders to update sprite’s location. One of my thoughts was using defos’ functions set_cursor_clipped and set_cursor_pos but I don’t fully understand the way they work. If I try to get mouse coordinates’ via defos.get_cursor_pos in fullscreen I always get (960, 540) coordinates. In windowed mode however I get something that resembles actual coordinates but those numbers are incorrect as well. How do I limit mouse cursor to the windows/screen? How does the defos.get_cursor_pos work?

defos.set_cursor_clipped(true) should be all you need. What platform are you using? It seems to work fine for me on windows.

defos.get_cursor_pos() will give you the coordinates of the mouse on your screen and defos.get_cursor_pos_view() will give you the coordinates of the mouse inside the game window. If you are in fullscreen they should both give you the same thing. I’m not sure why you would need these though, if you are doing gameplay stuff with the mouse, you can just use Defold’s input system.

Thank you for your response. I`m on Windows right now.

I’ve tried previously and after your message to use defos.set_cursor_clipped(true), but unfortunately it doesn`t work. I dunno why. I’ve tried it in gui_script of my cursor and in main.script.

And about defos.get_cursor_pos() and defos.get_cursor_pos_view(). They every time give me the same coordinates (960, 540). Thats why i was wondering about it. I use mouse do interact with ingame interface.

Hmm, and you’re not doing anything else with defos in other scripts, like defos.set_cursor_locked or anything like that? The mouse moves fine, it just doesn’t stay within the window? Does it the clipping work in windowed mode?

I don`t think so. I use only defos.set_cursor_clipped(true). I’m gonna check again.

Yes. Mouse moves fine and can go beyond the window. In windowed mode it works by default even without defos. So i can’t see any changes with defos.set_cursor_clipped(true) or without it in both fullscreen and windowed mode.