Mouse.GetState() always returning 0,0, no matter what

Hi guys
I am having an issue with trying to get the Mouse.GetState(). It’s always returning 0,0

I tried just the usual way of getting the Mouse.GetState() through MouseState mousestate = Mouse.GetState(); at the Update()

Then assigning mousestate.X and mousestate.Y to a Vector2 that’s declared outside as static. I am always getting 0,0 when trying to debug. I even just tried to dump mousestate.X and mousestate.Y at the Draw() at Window.title and still is 0,0.

I am running the game in a border-less Windows. Now, what I found is that at the beginning of the game (Initialize), both Mouse.WindowHandle and Window.Handle are the same value.

BUT in Update(), Mouse.WindowHandle = Window.Handle are different.
As far as I know, Mouse.WindowHandle {set} is not implemented in Monogame

Any remote idea what this is ? I’ve tried with monogame 3.7.1 and happens the same.

The mouse is just a regular USB mouse, I tried on a laptop and happens the same with the Touchpad.

Thanks.
Diego

After struggling a little more, I found out that .GetState() has an override that allows passing a GameWindow object.

So the solution to the problem, if anybody comes into a situation like this is simply putting:

Mouse.GetState(Window); in the Update() Method

:slight_smile:

This is a Windows DirectX project, right? Do you create other windows in the same process?
Mouse.GetState() should work here.

Yes, it’s a Windows DX project (not UWP), using monogame 3.6. But as said in the description, the problem is that GetState() was using a different handle rather than the window Handle.

GetState() uses Mouse.WindowHandle and in my case, both Mouse.WindowHandle and Window.Handle are different at the moment of the Update()

The solution was simply passing Window to the GetState(), so:

MouseState mouseState = Mouse.GetState(Window);

It’s an interesting case, I think. In spite it’s fixed now, this is the code to reproduce the issue:

    Program.cs
    [STAThread]
            static void Main() 
            {
               var objmain = new Main();
               objmain.Run();
            }

    Main.cs
            public Main()
                    {
                        /* Class constructor */
                        graphics = new GraphicsDeviceManager(this);             
                    }

    protected override void Initialize()
            { 
    graphics.HardwareModeSwitch = false;
    if (!graphics.GraphicsDevice.Adapter.IsWideScreen)
                                settings.tSettingsResolution = "1280x1024"; /* 4:3 */
                            else
                                settings.tSettingsResolution = "1280x720"; /* 16:9 */

string[] tSettingsResolutionaux;
                    tSettingsResolutionaux = settings.tSettingsResolution.Split('x');

    graphics.PreferredBackBufferWidth = Convert.ToInt16(tSettingsResolutionaux[0]);
                        graphics.PreferredBackBufferHeight = Convert.ToInt16(tSettingsResolutionaux[1]);
                        graphics.ApplyChanges();

    if (!settings.fSettingsWindowed)
                            graphics.ToggleFullScreen();

/* clear screen */
                    GraphicsDevice.Clear(Color.Black);
}

I had a fix for Mouse.WindowHandle a while ago.
If you really need, you can build monogame from source and apply this patch yourself.

Hi Nkast, thanks for your answer. Best solution I found was passing window to the getstate(), so:

MouseState mousestate = Mouse.GetState(Window);

This fixed the issue, as it passes the Window handle.
Another solution would be upgrading to 3.7.1 which, as far as I read, implemented {set} for Mouse.WindowHandle, so at the initialize part could be just used:

Mouse.WindowHandle=Window.Handle;

I think my solution is good, I couldn’t find it anywhere here on the forums, nor somewhere else. I mean, it’s an alternative as well for those not wanting to use 3.7.1 nor wanting to recompile monogame.

And it works like a charm. :slight_smile:

1 Like