[Tutorial/Resource] UI Toolkit: How to get the pressed mouse button from a MouseMoveEvent

Hi all, just a little resource / tutorial on how to check whether or not the left mouse button is pressed in a MouseMoveEvent in UI Toolkit (also as a note to myself).

I know the linked bug report is old but it seems the problem still exists in UI Toolkit (evt.button is always 0 in MouseMove events in the Editor). Here is how to work around that:

yourElement.RegisterCallback<MouseMoveEvent>(onMouseMove);

private void onMouseMove(MouseMoveEvent evt)
{
    // Sadly "evt.button == 0" does NOT work here, it's always 0, see bug report:
    // https://issuetracker.unity3d.com/issues/movemouseevent-dot-button-returns-0-both-when-no-button-is-pressed-and-when-the-left-mouse-button-is-pressed
    // We have to use the more accurate evt.pressedButtons Bitmask instead.
    bool isLeftButtonPressed = (evt.pressedButtons & 1) > 0;
    
    /// Enjoy :-)
}

Here is an Extension class for convenience:

using UnityEngine;
using UnityEngine.UIElements;

namespace Kamgam
{
    /// <summary>
    /// This extension adds reliable mouse button methods to IMouseEvents.<br />
    /// // Sadly "evt.button == 0" does NOT always work. Example: for MouseMove event it's always 0, see bug report:
    /// https://issuetracker.unity3d.com/issues/movemouseevent-dot-button-returns-0-both-when-no-button-is-pressed-and-when-the-left-mouse-button-is-pressed
    /// We have to use the more accurate evt.pressedButtons Bitmask instead.
    /// See: https://discussions.unity.com/t/tutorial-resource-ui-toolkit-how-to-get-the-pressed-mouse-button-from-a-mousemoveevent/1554470
    /// </summary>
    public static class IMouseEventExtensions
    {
        /// <summary>
        /// Returns -1 if no button has been found (checks for the first 6 buttons). Returns values are compatible with
        /// the .button property, see: https://docs.unity3d.com/ScriptReference/UIElements.IMouseEvent-button.html
        /// </summary>
        /// <param name="evt"></param>
        /// <returns></returns>
        public static int GetButton(this IMouseEvent evt)
        {
            // Left
            if ((evt.pressedButtons & 1) > 0)
                return 0;

            // Right
            if ((evt.pressedButtons & 2) > 0)
                return 1;

            // Middle
            if ((evt.pressedButtons & 4) > 0)
                return 2;

            // Other
            if ((evt.pressedButtons & 8) > 0)
                return 3;
            if ((evt.pressedButtons & 16) > 0)
                return 4;
            if ((evt.pressedButtons & 32) > 0)
                return 5;

            return -1;
        }

        public static bool IsLeftPressed(this IMouseEvent evt)
        {
            return (evt.pressedButtons & 1) > 0;
        }

        public static bool IsRightPressed(this IMouseEvent evt)
        {
            return (evt.pressedButtons & 2) > 0;
        }

        public static bool IsMiddlePressed(this IMouseEvent evt)
        {
            return (evt.pressedButtons & 4) > 0;
        }
    }
}