mouseeventdom-eventsblazorcapture

Blazor. How to capture mouse in onmousemove event?


How to capture mouse in onmousemove event in Blazor like UIElement.CaptureMouse() in WPF?


Solution

  • ... in Blazor like UIElement.CaptureMouse()

    The closest match would be Element.setPointerCapture()

    In order to use it you will need to get an ElementReference in Blazor with @ref and a JS method to invoke setPointerCapture on it. You need to pass a pointerId that you get from PointerEventArgs.

    Do not use the Mouse* related events/methods, they are more or less deprecated in JS.

    So you can start with something like:

     <div @ref="myTarget" @onpointerdown="StartCapture">  ... </div>
    
    @code{    
        ElementReference myTarget;
    
        async Task StartCapture(PointerEventArgs args)
        {
            await JSRuntime.InvokeVoidAsync("myJsFunctions.capturePointer",
                     myTarget, args.PointerId);
        }    
    }