SDL2 MOUSEBUTTONDOWN & MOUSEMOTION Event Bug!

SDL2.0 (Windows)

I believe I’ve found a bug with mouse events which I’ve been able to re-produce in my app and other apps that captures MOUSEBUTTONDOWN and MOUSEMOTION events.

The Basics:

Code:

// For Simplicity
SDL_Event event;

while(SDL_PollEvent(&event) != 0)
{
switch (event.type)
{
case SDL_MOUSEBUTTONUP:

    // If the left mouse button release, reset flag to false!
    if( event.button.button == SDL_BUTTON_LEFT )
    {
        isMouseSelection = false;

        //Debuggin: Get the mouse offsets
        std::cout << "Mouse released at: " << event.button.x << "," << event.button.y << std::endl;
    }
    break;
            
case SDL_MOUSEBUTTONDOWN:

    // We want mouse down on left button only.
    if( event.button.button == SDL_BUTTON_LEFT )
    {
        // Mark extra flag that the mouse down has executed.
        isMouseSelection = true;  

        //Debuggin: Get the mouse offsets
        std::cout << "Mouse pressed at: " << event.button.x << "," << event.button.y << std::endl;
    }
    break;
            
case SDL_MOUSEMOTION:

    // We only want Motion when the Left button is down!
    // And isMouseSelection is TRUE
    if( event.button.button == SDL_BUTTON_LEFT
        && isMouseSelection)
    {
        std::cout << "Mouse moved by: "
            << event.motion.xrel << ", " << event.motion.yrel
            << " : "
            << event.motion.x << "," << event.motion.y
            << std::endl;
    }
    break;

}
}

Pretty simple, On left button down, we set isMouseSelection = true. On release we set isMouseSelection = false.
Only when isMouseSelection == true & LEFT mouse is down, should the MOUSEMOTION be captured.

The problem:
If your in a window (not full screen) and change focus to the DESKTOP or another window

  1. Left Click outside of the SDL window to change the focus to the desktop or another program.
  2. Left Click back on your programs windows (But click the Window Title So your not clicking on the actual canvas, your clicking on the window border itself at the TOP).

When moving your mouse around the canvas this appears to register the active MOUSEMOTION event for the LEFT button. (meaning SDL thinks the left mouse button is still being held down).

As I move the mouse around the following is somehow showing true and displaying the MOTION output over the entire canvas until i reset it by clicking and releasing the LEFT Mouse button again.

Both the Left Mouse Button, and the Variable only set in the MouseDown event is active!

Code:

event.button.button == SDL_BUTTON_LEFT
&& isMouseSelection

I can see that clicking the window title might execute a MOUSEBUTTONDOWN event, but it should also register a MOUSEBUTTONUP on the release which it doesn’t doing.
It should also not be showing event.button.button == SDL_BUTTON_LEFT in the MOUSEMOTION event becasue the button is not being held down, only the program title was clicked and released for focus.

This is leaving the mouse event active the entire time which i believe should be looked into and corrected.

I hope I’ve explained this clearly.

Some Extra Notes:

Even if you put some extra checking on SDL_WINDOWEVENT_FOCUS_GAINED or LOST.
This only catches maybe 50% then without it. The issues are still there when clicking DESKTOP, Then Window Title, DESKTOP, then Window Title.

Also what is odd, is when you click the window title and regain focus, it never gives the output in the debug window from the MOUSEBUTTONDOWN event

ex…

Code:

case SDL_MOUSEBUTTONDOWN:

    // We want mouse down on left button only.
    if( event.button.button == SDL_BUTTON_LEFT )
    {
        // Mark extra flag that the mouse down has executed.
        isMouseSelection = true; 

        //Debuggin: Get the mouse offsets
        std::cout << "Mouse pressed at: " << event.button.x << "," << event.button.y << std::endl;
    }
    break;

I don’t see the std::cout printed the the screen. So something is off I believe, and when I get more time i’ll have to dig through the actual event code in SDL.

I added some more SDL Logging and found the following:

If you click on a canvas a several times, then click out of the window (desktop). Then click back on the Applications title bar you get all of the following events

INFO: Window 1 gained keyboard focus
INFO: Mouse entered window 1
Mouse pressed at: 490,0
INFO: SDL_MOUSEBUTTONDOWN 1 shown
Mouse moved by: 3, -18 : 493,0

So you get a:

  1. Gained Keyboard focus
  2. Window entered, which should only happen after you move over the canvas.
  3. Mouse Pressed showing 1 = LEFT BUTTON
  4. Extra Mouse Movement

This is from a single click and release on the Window Title with no movement over the canvas. Keep in mind there is still no MOUSEBUTTONUP for a release so this state stays in motion.
Then if you click on the canvas, you do not get another MOUSEBUTTONDOWN, only a MOUSEBUTTONUP for a release.

I hope someone else will give this a shot and confirm my finding sometime. I believe this will be an issue for anyone using the mouse and holding down the button and dragging for tasks, and if they leave the window and click back on the title bar which a lot of people do and their button and motions are still considered active it will cause issues.

Thanks for your help.

I’ve had another developer confirm the bug for me so I’ve created a bugzilla entry.

https://bugzilla.libsdl.org/show_bug.cgi?id=2842

There is also a main.cpp source file attached to the bug report giving proof of concept.

Thanks/