Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why MouseMove event occurs after MouseUp event?

In WindowsForms I just added event handlers as follows:

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        Debug.WriteLine($"=> Form1_MouseDown, Clicks: {e.Clicks}, Location: {e.Location}");
    }

    private void Form1_MouseUp(object sender, MouseEventArgs e)
    {
        Debug.WriteLine($"=> Form1_MouseUp, Clicks: {e.Clicks}, Location: {e.Location}");
    }

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        Debug.WriteLine($"=> Form1_MouseMove, Clicks: {e.Clicks}, Location: {e.Location}");
    }

And the output is:

=> Form1_MouseMove, Clicks: 0, Location: {X=17,Y=21}
=> Form1_MouseDown, Clicks: 1, Location: {X=17,Y=21}
=> Form1_MouseUp,   Clicks: 1, Location: {X=17,Y=21}
=> Form1_MouseMove, Clicks: 0, Location: {X=17,Y=21}

You can see that all events occurs in the same location, So my question is why is there a MouseMove event after MouseUp event?

Also I tried similar code in WPF and MouseMove event NOT occurred.

And I tried similar code in C++ and MouseMove event NOT occurred:

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
    switch (message)
    {
     ...

    case WM_MOUSEMOVE:
        OutputDebugString(L"WM_MOUSEMOVE\n");
        break;

    case WM_LBUTTONDOWN:
        OutputDebugString(L"WM_LBUTTONDOWN\n");
        break;

    case WM_LBUTTONUP:
        OutputDebugString(L"WM_LBUTTONUP\n");
        break;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
    return 0;
}
like image 758
google dev Avatar asked Jul 11 '19 18:07

google dev


People also ask

Which of the following event triggers after Mousedown and then mouseup over the same element if the left mouse button was used?

click. Triggers after mousedown and then mouseup over the same element if the left mouse button was used. dblclick. Triggers after two clicks on the same element within a short timeframe.

What is the difference between mouseup and Mousedown?

MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.

How does Mousemove work?

MouseMove is a simple event that is executed when a pointer is moving over or around an element. Mousemove is a javascript event that inside a web page. The mousemove event can also be understood as a part of an event handler, whereupon some action or movement by a mouse pointer, an intended script is executed.

What is Mousemove event?

The mousemove event is fired at an element when a pointing device (usually a mouse) is moved while the cursor's hotspot is inside it.

What are mouseDown and mouseUp events?

MouseDown, MouseUp events. Occur when the user clicks a mouse button. MouseDown occurs when the user presses the mouse button; MouseUp occurs when the user releases the mouse button.

What are the events of a mouse move?

We’ve already seen some of these events: Mouse button is clicked/released over an element. Mouse pointer comes over/out from an element. Every mouse move over an element triggers that event. Triggers after mousedown and then mouseup over the same element if the left mouse button was used.

What is the MouseMove event in HTML5?

The mousemove event is fired at an element when a pointing device (usually a mouse) is moved while the cursor's hotspot is inside it. The following example uses the mousedown, mousemove, and mouseup events to allow the user to draw on an HTML5 canvas. Its functionality is simple: the thickness of the line is set to 1, and the color is always black.

What triggers the event when you mouse over an element?

Every mouse move over an element triggers that event. Triggers after mousedown and then mouseup over the same element if the left mouse button was used. Triggers after two clicks on the same element within a short timeframe.


3 Answers

If your mouse had previously been focused on a separate window, then clicking on a new window and shifting the focus of the mouse will generate a mouse move event (even if the mouse didn't move immediately before or after you clicked your mouse).

Here is a link to a similar StackOverflow response "Ghost" MouseMove Event

like image 80
Luke Avatar answered Sep 22 '22 05:09

Luke


This is because the mouse capture by the MouseDown is released on MouseUp. And this extra MouseMove may be to ensure the cursor position. As a workaround you can do this

        Point LastLocation = Point.Empty;

        private void Form1_MouseDown(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("=> Form1_MouseDown, Clicks: " + e.Location + ", Location: " + e.Location + "");
        }

        private void Form1_MouseUp(object sender, MouseEventArgs e)
        {
            Debug.WriteLine("=> Form1_MouseUp, Clicks: " + e.Location + ", Location: " + e.Location + "");

        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (LastLocation != e.Location)
            {
                LastLocation = e.Location;
                Debug.WriteLine("=> Form1_MouseMove, Clicks: " + e.Location + ", Location: " + e.Location + "");
            }
        }
like image 39
Vishnu Babu Avatar answered Sep 24 '22 05:09

Vishnu Babu


This is the intended behavior and will also be trigger whenever app is being switched (Eg: Alt+Tab).

You should go with workaround as suggested by @VishnuBabu's workaround. And to ignore initial mousemove trigger, you can get the current position of cursor once window is loaded instead of setting the LastLocation to Empty.

like image 43
Naveen Kumar V Avatar answered Sep 20 '22 05:09

Naveen Kumar V