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

Author Topic: Mouse input  (Read 1606 times)

0 Members and 1 Guest are viewing this topic.

natchos

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
    • Email
Mouse input
« on: October 23, 2012, 09:15:07 am »
Hey I was wondering whether there is something wrong with the SFML mouse input class.
When I get input from the keyboard I use a pollEvent and a switch type structure

while(App.pollEvent(Event))
{
        switch(Event.type)
        {
                case sf::Event::Closed:
                        App.close();
                        break;
                case sf::Event::KeyPressed:
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        {
                                //Do stuff
                        }
                        break;
        }
}

And it works fine.

However attempting to put in a
case sf::Event::MouseButtonPressed: doesn't work.
In my application I did it like this

while(App.pollEvent(Event))
{
        switch(Event.type)
        {
                case sf::Event::Closed:
                        App.close();
                        break;
                case sf::Event::KeyPressed:
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        {
                                //Do stuff
                        }
                        break;
                case sf::Event::MouseButtonPressed:
                        if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
                        {
                                //Do stuff
                        }
        }
}

But it never triggered/worked. Instead I had to do this:

while(App.pollEvent(Event))
{
        switch(Event.type)
        {
                //Process events
        }
}
if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
        //React to the mouse button
}
Am I doing an incorrect implementation or is it something else?
« Last Edit: October 24, 2012, 05:04:54 pm by natchos »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Mouse input
« Reply #1 on: October 23, 2012, 09:48:19 am »
First, direct input and events are two totally different things, it doesn't make sense to use sf::Keyboard or sf::Mouse in an event loop. As explained in the tutorial.

And... how did you manage to get this crazy indentation? There are less than 10 lines of code but it's already unreadable ???

And please post a complete/valid piece of code, because those "if (...) // Do stuff   case ..." can be interpreted differently than what's really inside your program (the case is in the if) and won't help us to help you ;)
« Last Edit: October 23, 2012, 09:50:22 am by Laurent »
Laurent Gomila - SFML developer

natchos

  • Jr. Member
  • **
  • Posts: 97
    • View Profile
    • Email
Re: Mouse input
« Reply #2 on: October 24, 2012, 05:06:59 pm »
Well, as I said, the structure actually works fine for keyboard input. It hasn't given missed a single stroke so far. The reason for the crazy indentation was that since the actual structure is some hundred or so lines long, I didn't feel like burdening you with all the unneccesary code, and instead just copy pasted. I however discovered that attempting to use tab to create indentations in the webpage didn't work so I instead attempted to use spaces. It obviously didn't work.

Edit: Fixed the original post.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10281
    • View Profile
    • development blog
    • Email
Re: Mouse input
« Reply #3 on: October 24, 2012, 05:14:14 pm »
Well, as I said, the structure actually works fine for keyboard input.
So? Everyone gets lucky once. ;)

It seems like the event and real-time system have an equally fast reaction, where as for the mouse input the real-time system is faster. But that's just some blind guessing and as Laurent said, the systems are completely different and should not get mixed. Use the second version and you'll be find. :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://dev.my-gate.net/