Mouse goes off screen

I have been developing a game. Normally it defaults to desktop size, fullscreen. However, when testing other resolutions today I found in fullscreen mode the mouse can go out of the screen bounds. for example, say the desktop is 1600 but I set the fullscreen resolution to 1024x768. The mouse can go past the edge of the screen to pixel 1599. How do I set the mouse bounds to the screen bounds?

GraphicsDevice.Width/Height?

The GraphicsDevice Width and height are 1024 x 768 and all other aspects of the program respect this. Things are drawn correctly. It is just you can drag the mouse pointer outside the screen.

The mouse can go to the right as far as the width of the desktop resolution and to the bottom as far as the desktop resolution.

Can you clarify, this is in full screen with no borders etc. right?

Yes. This is in fullscreen mode, with not borders and resolution set to 1024x768, which is significantly smaller than the desktop.

So you want to restrict/clip the mouse-cursor to the bounds of your window?
Oh. And that is in fullscreen mode, right? Not borderless full-screen window? Because that would make a difference.

There’s an open issue for mouse capture on GitHub: https://github.com/MonoGame/MonoGame/issues/4982

Lots of mouse issues these days…?

Anyways, you can do the shooter cross-hair, or pong-paddle trick… Use mouse-position values to get changes, and add those changes to a vector2 called mousePos… multiply with any sensitivity factor you like…

Clip mousePos to the screen-bounds, and draw a cursor sprite on it…

[DllImport("user32.dll")]
static extern void ClipCursor(ref Rectangle rect);


protected override void Update(GameTime gameTime)
{
  // TODO: Add your update logic here
  if (IsActive)
  {
    Rectangle rect = Window.ClientBounds;
    rect.Width += rect.X;
    rect.Height += rect.Y;
    
    ClipCursor(ref rect);
  }
  
  base.Update(gameTime);
}

Windows only

kosmonautgames wrote:

[DllImport(“user32.dll”)]
static extern void ClipCursor(ref Rectangle rect);

protected override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
if (IsActive)
{
Rectangle rect = Window.ClientBounds;
rect.Width += rect.X;
rect.Height += rect.Y;

ClipCursor(ref rect);

}

base.Update(gameTime);
}

Windows only

Thanks, that works perfectly. However, I noticed your code calls it every update. My implementation refines slightly by calling ClipCursor once when the graphics system is initialised (if in full screen mode). Thereafter instead of running the snipped every update I attach it as a handler to the Game.Activated event. It is called whenever the game client window (re)gains focus and avoids wasting precious runtime in Game.Update.

Many thanks for the solution. :smile:

I was on mobile, but that was the first link I found when googling clipCursor xna, i just posted the code since I think it’s the same thing I use

The way I do it in my game is have an event when switching to isActive to call this code

So in the main game initialization i simply have

        Activated += IsActivated;
        Deactivated += IsDeactivated;

And then consequently

private void IsActivated(object sender, EventArgs e)
{
    isActive = true;

  ... some stuff
}