Blazor mouse events and mouse capture

Tomaz Koritnik 21 Reputation points
2021-10-01T07:44:59.387+00:00

I'd like to create a panel with movable html elements using Blazor WebAssembly (for something similar like a workflow editor). Currently I have a <div> with position:absolute style as a movable object where I handle mouse events on it. It works, but as soon as the mouse goes out of this div or out of the browser window, events are not sent to the div anymore. I need to capture the mouse somehow so that events are sent to this div in all cases, even if mouse cursor is outside the browser window. How can I accomplish this? Maybe I need to handle the events on a document-level somehow, maybe using JS interop, I don't know.

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,396 questions
0 comments No comments
{count} votes

Accepted answer
  1. Bruce (SqlWork.com) 56,846 Reputation points
    2021-10-02T15:17:06.233+00:00

    As blazor always depends on json interop for events even for the builtin ones there is nothing wrong with your approach. You could make a generic blazor component for capturing window or root dom events.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Tomaz Koritnik 21 Reputation points
    2021-10-01T08:59:37.72+00:00

    I actually found a way using JSInterop. I have defined a custom JavaScript function in index.html that calls Element.setPointerCapture(). Is this the only way that this could be accomplished?

    0 comments No comments