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

Author Topic: About key combinations  (Read 4188 times)

0 Members and 1 Guest are viewing this topic.

fabvman

  • Newbie
  • *
  • Posts: 11
    • View Profile
About key combinations
« on: December 29, 2010, 03:48:08 am »
Hi, I'm using the object Event to handle keyboard events and I need to do some things when I press simultaneously 'a' and 'd'. How can I do such thing?
Another question, if I use the object input to handle keyboard events ¿Is there some way to know when a key is released?
Thanks!

model76

  • Full Member
  • ***
  • Posts: 231
    • View Profile
About key combinations
« Reply #1 on: December 29, 2010, 05:46:45 am »
Depends on whether you want to do something as long as both keys are down, or just the moment they are pressed.

The first is real easy - look at Input in the tutorial. It's in the chapter about event handling. Just be aware that Input in SFML 1 is not synchronized with events, meaning that Input may tell you that a key is down, before you get the corresponding key pressed event. So this can cause problems under certain circumstances,  if you mix events with input.

The second is a little more tricky, as key pressed events are handled in the event loop, one key after the other, and never at the same time. This raises a few more questions: Should the thing happen when 'A' is held down, and 'D' is then pressed? Should the thing happen when 'D' is held down, and 'A' is then pressed? Or should the thing only happen if 'A' and 'D' are pressed down at approximately the same time?


And to the second question:
Input only tells you about the state of things, such as whether a key or mouse button is down or not, or where the mouse cursor is on the screen.
Events refer to the moment when keys are pressed or released, when the mouse cursor is moved, window looses focus, etc.

fabvman

  • Newbie
  • *
  • Posts: 11
    • View Profile
Thanks
« Reply #2 on: December 29, 2010, 05:12:54 pm »
Hi! What I wanted to do is to stop my character when I just press (not while I'm pressing) 'a' and 'd' keys simultaneously (assuming that the character is still moving). I finally use Input. Something like this:

Code: [Select]
if(input.isKeyDown(sf::keys::a) && input.isKeyDown(sf::keys::d))
{
      //stop character
}

But I can't achieve same behavior using Events. I try to take two events consecutively:

Code: [Select]
while(app.getEvent(Event))
{
     if(Event.type == sf::event::keyPressed)
     {
            if(Event.key.code == sf::key::a)
            {
                   app.getEvent(Event); //to read the next event

                   if(Event.type == sf::event::keyPressed)
                   {
                          if(Event.key.code == sf::key::d)
                          {
                                  //stop character
                           }
                   }

            }
      }
}


But I doesn't work. What am I doing wrong?

Thank you very much for your help!

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
About key combinations
« Reply #3 on: December 29, 2010, 07:23:29 pm »
This would work only if both events are consecutive in the event queue (which means that absolutely nothing happened between them), and you pressed A first and then D. Which is unlikely to happen, at least it won't happen everytime.

Moreover, if the second event is not sf::Key:: D KeyPressed, it won't be processed further.

These two events won't happen at the same time, so you obviously need to store a state (like "A is already pressed" and then catch the "D pressed" event, and vice-versa). Which is exactly what sf::Input does.
Code: [Select]
while (app.getEvent(Event))
{
     if (Event.type == sf::Event::KeyPressed)
     {
            if ((Event.Key.Code == sf::Key::A && input.IsKeyDown(sf::Key::D)) ||
                (Event.Key.Code == sf::Key::D && input.IsKeyDown(sf::Key::A)))
            {
                // stop character
            }
      }
}
Laurent Gomila - SFML developer

 

anything