SFML community forums

Help => General => Topic started by: mkalex777 on November 11, 2015, 07:04:35 pm

Title: false trigger of mouse event
Post by: mkalex777 on November 11, 2015, 07:04:35 pm
I'm using mouse button in my game to capture control of some entity. so, while mouse button is pressed I can move entity. And it works well.
But I catch some weird bug, sometimes when I hold mouse button pressed, it rases several pressed/released events, so I lose control on desired entity. It happens rare, so I cannot catch it under debugger. I added logging and all what I can see that there are some false triggered pressed/release event. Is there any way to prevent fire of unexpected pressed/released event when mouse button state actually didn't changed and it is still pressed?
Title: Re: false trigger of mouse event
Post by: Hapax on November 11, 2015, 08:29:04 pm
Are you creating a new sf::Event on each frame?
Title: Re: false trigger of mouse event
Post by: Laurent on November 11, 2015, 09:08:39 pm
We can't help you unless you provide a complete and minimal example that reproduces the problem.

Also, make sure that you're not mixing different versions of SFML/CSFML/SFML.Net.
Title: Re: false trigger of mouse event
Post by: mkalex777 on November 11, 2015, 09:16:20 pm
It's not easy to reproduce it, because it may works without such bug for a long time. But sometimes it starts to happen. Probably it appears when there are a lot of render objects on the screen ( rectangle & circle shapes, text), but I'm not sure that it's related. Also I've seen that when I pressing some buttons on the keyboard (space for example) it happens more frequently. Actually it's float bug, so I can't catch it in the debugger.
Title: Re: false trigger of mouse event
Post by: Nexus on November 11, 2015, 09:23:28 pm
I'm sure you can understand that we have no chance of helping you with such a description ;)
Title: Re: false trigger of mouse event
Post by: Arcade on November 11, 2015, 11:48:26 pm
You may also want to do a quick check and make sure it isn't a hardware problem with your mouse. Sometimes when mice get worn out the buttons can start to "bounce". Do you have another mouse you can try to reproduce the problem with?
Title: Re: false trigger of mouse event
Post by: mkalex777 on November 12, 2015, 05:50:12 pm
You may also want to do a quick check and make sure it isn't a hardware problem with your mouse. Sometimes when mice get worn out the buttons can start to "bounce". Do you have another mouse you can try to reproduce the problem with?

I implemented some check and it seems that it's SFML issue. I added check of actual mouse state from WinAPI. And sometimes SFML report about released key when WinAPI reports that key is still pressed.

Here is an example:
        private static class NativeMethods
        {
            [System.Runtime.InteropServices.DllImport("user32")]
            [System.Security.SuppressUnmanagedCodeSecurity]
            public static extern short GetAsyncKeyState(int vKey);

            public const int VK_LBUTTON = 1;
            public const int VK_RBUTTON = 2;
            public const int VK_MBUTTON = 4;
        }

        private void Window_OnMouseButtonReleased(object sender, MouseButtonEventArgs e)
        {
            if (e.Button == Mouse.Button.Left &&
                NativeMethods.GetAsyncKeyState(NativeMethods.VK_LBUTTON)!=0)
            {
                Logger.Warn("Window_OnMouseButtonReleased: false trigger");
                return;
            }
       }
 

result:
[ WARN] Window_OnMouseButtonReleased: false trigger
 

There is need for a long time testing to catch it, because it didn't trigger false event just after app startup.

UPDATE: it seems that I found the reason, I used Mouse.GetPosition in the render loop. I assume that this call may disrupt some internal SFML state and tried to remove it.
I replaced it with position stored in the local variable and set in the MouseMooved event. And yes! It seems that this weird bug disappears.

UPDATE2: no, it still reproduced. Needs to check my mouse to make sure that it's not hardware issue
Title: Re: false trigger of mouse event
Post by: Laurent on November 12, 2015, 08:32:17 pm
Quote
Need some time to make sure that it won't appears again.
You can also make sure that it always happens with the old code. Now that you know what triggers it, maybe it will happen more frequently in a code that does just that.

Before labeling this as a bug, we need to make sure that it doesn't happen only in your environment. We also need to check wether it's a bug in SFML.Net, CSFML or SFML.