MouseState not changing in Custom game loop (no Game class)

On Windows, using MonoGame.Framework.WindowsDX 3.7.0.944.
(Windows 10, but not “UWP” app; Surface Pro (2017))

If use MonoGame template, the following code in Game.Update override sees changed MouseState each loop.
C#:

        MouseState mouseState = Mouse.GetState();
        if (mouseState.X != 0)
        {
            int i = 1;   // Breakpoint here.
        }
        if (mouseState.RightButton == ButtonState.Pressed)
        {
            int i = 1;   // Breakpoint here.
        }

Putting the same code into a desktop application that uses custom code to do the looping (no Game class), the MouseState never changes from default (0,0), no buttons pressed.

VB (breakpoints never hit):

    Dim mouseState As MouseState = Mouse.GetState
    If mouseState.X <> 0 Then
        Test()
    End If
    If mouseState.RightButton = ButtonState.Pressed Then
        Test()
    End If

NOTE: FrameworkDispatcher.Update() is called per loop; adding to the code used in this topic.

Q: What do I need to do to initialize the Mouse class?

That may need to be something we need to clean up/clarify. Input handling should likely be handled more by the FrameworkDispatcher than the Game class. The vast majority of people use the Game class, so this input issue hasn’t really come up before.

Looking at MonoGame source code, I might be able to create a subclass of GameWindow, and call Mouse.GetState(GameWindow window).

But its probably easier to shelve this attempt.

I’ll see if I can adapt my code to use Game class. If that is too much trouble, I’ll access mouse without using XNA.

Looking at old XNA-using code, THIS line should make Mouse.GetState work (relative to the given handle) - but it doesn’t:

Mouse.WindowHandle = ...

I’ve filed an issue for this: https://github.com/MonoGame/MonoGame/issues/6009

1 Like