Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: PollEvent and double MouseButtonPressed  (Read 3057 times)

0 Members and 1 Guest are viewing this topic.

Torraske

  • Newbie
  • *
  • Posts: 6
    • View Profile
PollEvent and double MouseButtonPressed
« on: May 31, 2012, 04:21:56 pm »
Hello, my problem is very weird, I want to perform a simple "double click" (but triggered when I press the mouse button, not when I release it)

Problem: The PollEvent is not detecting me the second MouseButtonPressed (for a double click)

It is weird because it detects the second MouseButtonReleased, but not the MouseButtonPressed :S

Code:

            public void DispatchEvents()
            {
                Event e;
                while (PollEvent(out e)) // PollEvent Location
                {
                    CallEventHandler(e);
                }
            }
           
            void CallEventHandler(Event e)
            {
                switch (e.Type)
                {                    
                    case EventType.MouseButtonPressed:         // Doesn't detect double click            
                        if (MouseButtonPressed != null)  
                            MouseButtonPressed(this, new MouseButtonEventArgs(e.MouseButton));                        
                        break;

                    case EventType.MouseButtonReleased:       // Does detect doble click                
                        if (MouseButtonReleased != null)
                            MouseButtonReleased(this, new MouseButtonEventArgs(e.MouseButton));
                        break;
                   
                }
            }

 

Thanks!!

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: PollEvent and double MouseButtonPressed
« Reply #1 on: May 31, 2012, 05:42:11 pm »
At first I'd suggest you don't use using sf! Because it will make your code look really weired and confusing to other people...

Second I have no idea what class MouseButtonPressed is and what MouseButtonPressed(this, new MouseButtonEventArgs(e.MouseButton)); should do.

I would do something like (untested):
unsigned int MouseButtonPressed = 0;
//...
if(event.type == sf::Event::MouseButtonPressed)
{
        if(MouseButtonPressed < 2)
                ++MouseButtonPressed;
        else
        {
                MouseButtonPressed = 0;
                // Handle Double (or more) Click Event
        }
}
else if(event.type == sf::Event::MouseButtonReleased)
{
        MouseButtonPressed = 0;
}
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Torraske

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: PollEvent and double MouseButtonPressed
« Reply #2 on: May 31, 2012, 06:01:23 pm »
Thanks for your reply, my code looks weird because it is in C# (I dont like it too)

I have changed it to see if it is clearer:

(Note that the problem is in PollEvent, it doesnt send me the second button pressed)

            public void DispatchEvents()
            {
                Event e;
                while (PollEvent(out e)) // PollEvent Location
                {
                   if(e.type == EvenType.MouseButtonPressed)
                   {
                           if(MouseButtonPressed < 2)
                                   ++MouseButtonPressed; // Doesnt work, when I double click, the PollEvent seems to "eat" a click
                           else
                           {
                                   MouseButtonPressed = 0;
                                   // Handle Double (or more) Click Event
                           }
                   }
                }
            }
       
 

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: PollEvent and double MouseButtonPressed
« Reply #3 on: May 31, 2012, 06:10:04 pm »
What OS and revision of SFML 2 are you using?
Laurent Gomila - SFML developer

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
Re: PollEvent and double MouseButtonPressed
« Reply #4 on: May 31, 2012, 06:10:49 pm »
Oh wait no, I made that wrong. Because for a double click you'll have to press, release and press. With my code it will get reset when ever you release the button.

unsigned int MouseButtonPressed = 0;
//...
if(event.type == sf::Event::MouseButtonPressed)
{
        if(MouseButtonPressed < 2)
                ++MouseButtonPressed;
        else
        {
                MouseButtonPressed = 0;
                // Handle Double (or more) Click Event
        }
}
else if(event.type == sf::Event::MouseButtonReleased && MouseButtonPressed >= 2)
{
        MouseButtonPressed = 0;
}
 

But keep in mind you won't be able to handle single clicks anymore with that code and the user will be able to make double clicks with an unlimited pause in between.

I guess a better way would be to measure the time difference between two clicks with a sf::Clock. Whenever someone clicks check if the difference is not too big and then restart the clock.
« Last Edit: May 31, 2012, 06:21:37 pm by eXpl0it3r »
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Torraske

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: PollEvent and double MouseButtonPressed
« Reply #5 on: May 31, 2012, 06:39:50 pm »
Lurent:

I am not sure, because I have downloaded them with an engine (the sfml dll date is November 8th 2011 and the function hooked is PollEvent (not pollEvent))

However, I have downloaded the latest DLL and hooked it directly to pollEvent without luck.
The OS is Win7 x64

The engine is coded in C#, but it uses the csfml libs (not the sfml net). Could it make some trouble? The weird thing it works perfect for button release...


eXpl0it3r:

I am not concerned with the double click function right now, the problem I have is the event is not catched by PollEvent for some very weird reason.


Thanks guys, I have been several days trying to fix this issue and I am getting crazy



Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: PollEvent and double MouseButtonPressed
« Reply #6 on: June 03, 2012, 08:50:32 am »
Works perfectly for me with latest SFML.Net on Windows 7 x64.
Laurent Gomila - SFML developer

Torraske

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: PollEvent and double MouseButtonPressed
« Reply #7 on: June 04, 2012, 12:52:09 am »
Thanks for the reply, I am coding in C#, but I have the libraries directly imported from the "csfml" using:

            [DllImport("csfml-window-2", CallingConvention = CallingConvention.Cdecl)]
            [SuppressUnmanagedCodeSecurity]
            static extern bool sfWindow_PollEvent(IntPtr This, out Event Evt);
 

I will try anyway on other computers to see if the problem is my OS or graphic card

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: PollEvent and double MouseButtonPressed
« Reply #8 on: June 04, 2012, 08:08:10 am »
Since it works with SFML.Net and not with the engine's own binding, there's most likely an error in it. By the way, why don't they use SFML.Net?

Quote
I will try anyway on other computers to see if the problem is my OS or graphic card
Graphics card? No :)
OS? I tried with the same OS and it worked.
Laurent Gomila - SFML developer

Torraske

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: PollEvent and double MouseButtonPressed
« Reply #9 on: June 05, 2012, 11:24:22 pm »
Yes I think the problem may be there, I will try to replace the libraries.

The doesnt use the sfml-net because when they started to build the engine, the sfml-net were not created. So to exchange the libs now is doesnt worth the effort.

However, I cannot do anything without the f***ing double click!! And I want it when I press the second "MouseButtonPressed" not the common double click: the second "MouseButtonReleased"

Thanks for your replies

Torraske

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: PollEvent and double MouseButtonPressed
« Reply #10 on: June 06, 2012, 06:23:45 pm »
I realized the PollEvent is being overriden by the RenderWindow class, so it is calling the PollEvent from "csfml-graphics-2.dll" (Not window dll)

I am trying to use the latest sfml graphics lib to solve it.

Sorry for the previous unaccurate info

 

anything