Restrict mouse movement in game window

Hi there,

I tried all mouse scripts I could find over here (without assuming I found them all), and I never been able to restrict move cursor within the game window (when exporting a windows executable in framed window). The cursor always goes out of the windows. Is there a way to restrict the cursor within the window using Python or is it a OS setting ?

Thank you!

On the tutorial on, the script sets the cursor at the middle of the gme window.

Ya but the problem isn’t when using the mouse to rotate a camera (which is what is shown in the tutorial) but using the mouse to move a cursor on screen, using Rasterizer. The cursor will move correctly but if you move beyond the window’s boundaries, the cursor will go out of the window frame. Is there any way to keep the cursor within the window’s boundaries.

Oh, sorry, I read your pot wrong. I thought you were use a mouselook script.

from Rasterizer import getWindowWidth, getWindowHeight, setMousePosition #if you already have imports from Rasterizer, just tack these on
mouse = cont.sensors['mouse'] # this should be a mouse move sensor

mpos = mouse.position
window = [getWindowWidth(), getWindowHeight()]
if mpos[0] > window[0]: mpos = window[0]
if mpos[0] < 0: mpos[0] = 0
if mpos[1] > window[1]: mpos[1] = window[1]
if mpos[1] < 0: mpos[1] = 0


Well, it does work but it’s kinda jerky since you can easily see the mouse going out but being pull toward the window. I thought there would be a way to really keep the mouse focued on the current active window. Maybe it must be done within blender source code. Most commercial game don’t use the OS cursor over the game window and their cursor cannot go out of bound.

But thank you Captain it will do the job for now :slight_smile:

I think the jerkiness is OS-specific- windows uses some specific method to draw the cursor that seems to be a frame behind, and when a program sets the cursor position it draws where windows thought it should be, and then where the program puts it. I could be wrong, but I believe there’s nothing in blender you can do to fix it.