SFML community forums

Help => Window => Topic started by: trilavia on January 11, 2013, 02:52:56 pm

Title: Key combinations in SFML
Post by: trilavia 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?
Title: Re: Key combinations in SFML
Post by: G. on January 11, 2013, 03:32:13 pm
Use the sf::Keyboard (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Keyboard.php) 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.
Title: Re: Key combinations in SFML
Post by: thePyro_13 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 (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-actions.html) might be a better solution.
Title: Re: Key combinations in SFML
Post by: trilavia 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.
Title: Re: Key combinations in SFML
Post by: G. 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...)
Title: Re: Key combinations in SFML
Post by: trilavia 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.
Title: Re: Key combinations in SFML
Post by: Nexus 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
}