Disable Mouse Interaction But Detect Mouse Movement

Is there a way to disable the mouse’s ability to interact with the UI (without locking it), but still detect if the player is moving the mouse.

My use-case is that I want to hide/disable the cursor when the user interacts with the keyboard and then show/enable it when the player moves the mouse. The problem is that if I lock the cursor, it will snap to the center of the screen. I know I can use warp cursor to position it back to the previous position, but I want my code to be cross-platform and be able to be used on mobile and WebGL.

Is there anyway to do this?

Thanks!

One way:

Vector2 m_LastPosition;

//...

// Disable device in frontend but not in backend.
m_LastPosition = Mouse.current.position.ReadValue();
InputSystem.DisableDevice(Mouse.current,
    keepSendingEvents: true);
Cursor.visible = false;

// When position changes, re-enable mouse.
InputSystem.onEvent +=
    (eventPtr, device) =>
    {
        if (device is Mouse mouse && mouse.position.ReadValueFromEvent(eventPtr) != m_LastPosition)
        {
            InputSystem.EnableDevice(mouse);
            Cursor.visible = true;
        }
    };

Thanks for the quick reply! Unfortunately this doesn’t work as I can still click UI and hover states still work in UIToolkit. The only event I need is if the mouse moved. Setting “keepSendingEvents” to false, of course, will disable everything, but keeping it true still allows it to trigger UIToolkit states and clicks.

Am I missing something?

Thanks!

If UITK is used with InputSystemUIInputSystem, it should still work. But yeah, everything that isn’t picking up input through the input system will unfortunately not be affected by that method above. It’ll only cut off input at the Mouse device level (even with keepSendingEvents=false).

This is what I am using:

When you said “InputSystemUIInputSystem”, did you mean InputSystemUIInputModule? If so, should I report this as a bug since you mentioned it should work?

7914859--1009633--Screen Shot 2022-02-21 at 12.44.29 PM.png