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

Author Topic: Strange input behaviour (Checking two inputs at once)  (Read 3918 times)

0 Members and 1 Guest are viewing this topic.

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Strange input behaviour (Checking two inputs at once)
« on: September 01, 2011, 01:11:45 pm »
I've come across a bit of an odd problem while working on a simple editor for a shmup. I have several controls which require holding down a keyboard key and using the mouse (although it appears to happen with any combination of two inputs).

Code: [Select]
int main()
{
sf::RenderWindow window(sf::VideoMode(1024, 768, 32), "Blah");

sf::Vector2f pos(0, 0);

bool running = true;
while (running)
{
sf::Event ev;
while (window.PollEvent(ev))
{
if (ev.Type == sf::Event::Closed)
{
running = false;
}
}

if (sf::Keyboard::IsKeyPressed(sf::Keyboard::LShift) && sf::Mouse::IsButtonPressed(sf::Mouse::Left))
{
//this code can be executed by pressing Left mouse, then shift
pos.x = sf::Mouse::GetPosition(window).x;
pos.y = sf::Mouse::GetPosition(window).y;
}

window.Clear();

sf::Shape shape = sf::Shape::Circle(pos, 10, sf::Color::Red);
window.Draw(shape);

window.Display();
}

return 0;
}


The if statement will be executed by pressing both Shift and Left mouse, or pressing the mouse button and then shift. If I swap the Mouse button check and the LShift check (Mouse::Left && Keyboard::LShift), it works in the opposite order, which makes me think I'm doing something stupid.

I'd usually do more experimentation/research before posting an issue here, but I have a deadline this time :wink:

I'm using an out of date SFML2 build (end of July) - After the new input system (as you can see) but before the sf::Image/sf::Texture split.

OS: Windows Server 2008 R2

Any help would be great!

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Strange input behaviour (Checking two inputs at once)
« Reply #1 on: September 01, 2011, 01:25:04 pm »
On which OS do you work ?
SFML / OS X developer

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Strange input behaviour (Checking two inputs at once)
« Reply #2 on: September 01, 2011, 01:32:24 pm »
Sorry, should have put that in the original post. Windows Server 2008 R2.

Silvah

  • Guest
Strange input behaviour (Checking two inputs at once)
« Reply #3 on: September 01, 2011, 01:47:46 pm »
I'm unable to reproduce that behavior, are you sure you're actually releasing the mouse button before pressing Shift? Even if both are held together for a millisecond, it's very likely the condition will be true.

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Strange input behaviour (Checking two inputs at once)
« Reply #4 on: September 01, 2011, 01:50:08 pm »
Quote from: "Silvah"
are you sure you're actually releasing the mouse button before pressing Shift?


Definitely.

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Strange input behaviour (Checking two inputs at once)
« Reply #5 on: September 01, 2011, 01:58:37 pm »
Thought I should try this on another computer - same behaviour (Different keyboard/mouse, Windows XP SP3)

thePyro_13

  • Full Member
  • ***
  • Posts: 156
    • View Profile
Strange input behaviour (Checking two inputs at once)
« Reply #6 on: September 01, 2011, 02:15:59 pm »
Hmm, this happens when I tried it as well.

I'm running win7 64bit and using the latest snapshot.

EDIT: You can work around it using' button pressed' booleans like this:
Code: [Select]
#include "SFML\Graphics.hpp"

int main()
{
   sf::RenderWindow window(sf::VideoMode(1024, 768, 32), "Blah");

   sf::Vector2f pos(0, 0);

   bool running = true;
   while (running)
   {
 bool shift = false, M1 = false;
      sf::Event ev;
      while (window.PollEvent(ev))
      {
         if (ev.Type == sf::Event::Closed)
         {
            running = false;
         }
      }

      if (sf::Keyboard::IsKeyPressed(sf::Keyboard::LShift))
shift = true;  

 if(sf::Mouse::IsButtonPressed(sf::Mouse::Left))
M1 = true;

 if(M1 && shift)
 {
pos.x = sf::Mouse::GetPosition(window).x;
         pos.y = sf::Mouse::GetPosition(window).y;
 }

      window.Clear();
 sf::Shape shape;
 if(shift)
         shape = sf::Shape::Circle(pos, 10, sf::Color::Green);
 else if(M1)
shape = sf::Shape::Circle(pos, 10, sf::Color::Blue);
 else
shape = sf::Shape::Circle(pos, 10, sf::Color::Red);

      window.Draw(shape);

      window.Display();
   }

   return 0;
}


The code above acts as you wanted it to in the OP.

However so much as nesting these if statements reproduces the bug.

Code: [Select]
     if (sf::Keyboard::IsKeyPressed(sf::Keyboard::LShift))
 {
shift = true;  

   if(sf::Mouse::IsButtonPressed(sf::Mouse::Left))
   M1 = true;
 }

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Strange input behaviour (Checking two inputs at once)
« Reply #7 on: September 02, 2011, 05:49:55 am »
Thanks for the work-around.

Still a strange bug though.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Strange input behaviour (Checking two inputs at once)
« Reply #8 on: September 02, 2011, 11:03:47 pm »
It's fixed :)

Thanks for your feedback.
Laurent Gomila - SFML developer