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

Author Topic: Key combinations in SFML  (Read 4432 times)

0 Members and 1 Guest are viewing this topic.

trilavia

  • Newbie
  • *
  • Posts: 28
    • View Profile
Key combinations in SFML
« on: January 11, 2013, 02:52:56 pm »
Well, handling key combinations like when 'shift' and 'a' are held using real time input are easy peasy.
But I have a problem. I need combinations like "ctrl and left mouse button pressed at once". I can get mouse event at one loop and the ctrl event at the next loop. So it's hard to do. What's the good way of handling this?

G.

  • Hero Member
  • *****
  • Posts: 1554
    • View Profile
Re: Key combinations in SFML
« Reply #1 on: January 11, 2013, 03:32:13 pm »
Use the sf::Keyboard class to know if any given key is pressed at any given moment.
If you're not using 2.0 yet (you should ;) ), do it with the sf::Input class.

thePyro_13

  • Full Member
  • ***
  • Posts: 154
    • View Profile
    • http://cyangames.wordpress.com
Re: Key combinations in SFML
« Reply #2 on: January 11, 2013, 03:36:45 pm »
If i understand your question correctly then you should use the input classes for this rather than the event based ones.

Assuming you're on 2.0, this can be done by querying sf::Mouse and sf::Keyboard. Use sf::Input for 1.6.

Pseudocode for SFML2:
if(sf::Mouse::isButtonPressed(sf::Mouse::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::LShift))
    //Do stuff

Pseudocode for SFML1.6:
const sf::Input &input = window.GetInput();
if(input.IsMouseButtonDown(sf::Mouse::Left) && input.IsKeyDown(sf::Key::LShift))
    //Do stuff

As things get more complex using something like thor::Action might be a better solution.

trilavia

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Key combinations in SFML
« Reply #3 on: January 11, 2013, 03:46:50 pm »
No, I said I don't need the held (input class) approach as this one is trival. I just want one-time press (ie when I ctrl + click and held them, only one action gets triggered). So it's the event approach, but it must be tweaked in some way because I can get events at different frames and in different order.

G.

  • Hero Member
  • *****
  • Posts: 1554
    • View Profile
Re: Key combinations in SFML
« Reply #4 on: January 11, 2013, 04:01:53 pm »
What's wrong with checking with sf::Keyboard if ctrl is pressed when processing the MouseButtonPressed event? ??? (or/and the opposite, check with sf::Mouse inside the keyPressed event)
Only one action will be triggered, unless you release mouse click and click again of course.

Or do you want it to happen only when both events are triggered at the same frame? (highly unlikely to happen...)
« Last Edit: January 11, 2013, 04:03:49 pm by G. »

trilavia

  • Newbie
  • *
  • Posts: 28
    • View Profile
Re: Key combinations in SFML
« Reply #5 on: January 11, 2013, 04:04:34 pm »
Sorry, I missread your code! I thought there is input everywhere... Yes, your solutions solves the problem. Thanks.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Re: Key combinations in SFML
« Reply #6 on: January 11, 2013, 04:59:10 pm »
If you want to handle both realtime input and events uniformly, you can take a look at Thor's Actions I have written (see thePyro_13's link):
thor::ActionMap<std::string> map(...);
auto click = thor::Action(sf::Mouse::Left, thor::Action::PressOnce);
auto ctrl = thor::Action(sf::Keyboard::LCtrl) || thor::Action(sf::Keyboard::RCtrl);
map["ctrl-click"] = click && ctrl;

// in game loop
if (map.isActive("ctrl-click"))
{
    // do something
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: