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

Author Topic: sf::Keyboard  (Read 1218 times)

0 Members and 1 Guest are viewing this topic.

Klemens

  • Newbie
  • *
  • Posts: 2
    • View Profile
sf::Keyboard
« on: September 24, 2016, 12:43:30 am »
Hello ! I have a problem and I could not find a answer. I need handle a whichever key form keyboard (in specific case) and i dont know how to do it.
Below is code in c++.
void SnakeGame::initializeDirectionMap(){
    mapOfDirects[sf::Keyboard::Right] = std::shared_ptr<DirectConditions>(new GoRight);
    mapOfDirects[sf::Keyboard::Left] = std::shared_ptr<DirectConditions>(new GoLeft);
    mapOfDirects[sf::Keyboard::Up] = std::shared_ptr<DirectConditions>(new GoUp);
    mapOfDirects[sf::Keyboard::Down] = std::shared_ptr<DirectConditions>(new GoDown);
    mapOfDirects[sf::Keyboard::End] = std::shared_ptr<DirectConditions>(new Pause);
    mapOfDirects[sf::Keyboard::AnyKey] = std::shared_ptr<DirectConditions>(new Exit); // i don't know how to do it  
}

direction=event.key.code; // take key from keyboard
mapOfDirects[direction]->choosenDirection(*this);  // choose which method have to execute by key from keyboard
 
Is even possible handle all keys in sf::Keyboard::Key type ? I mean I press A,B,F5, whatever and always mapOfDirect[sf::Keyboard::"AnyKey"] will be choosen that is what I need. ("AnyKey" means my leak of knowledge :P I don't know what I have to put there instead of AnyKey)
If you know some tutorials or tips with events handle form keyboard which might help me solve this issue please share with me.
I would really appreciate help !
« Last Edit: September 24, 2016, 09:43:25 am by Klemens »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Keyboard
« Reply #1 on: September 24, 2016, 08:36:59 am »
You can't refer to "any key" directly. But you can handle the case of "direction not found in map" easily:

auto it = mapOfDirects.find(direction);
if (it != mapOfDirects.end())
{
    // direction found
    (*it)->choosenDirection(*this);
}
else
{
    // any other key
    // exit...
}
Laurent Gomila - SFML developer

Klemens

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: sf::Keyboard
« Reply #2 on: September 24, 2016, 10:32:53 am »
Awesome ! Thanks a lot ! But I have problem with cpp :P
 
std::map<sf::Keyboard::Key, std::shared_ptr<DirectConditions>>::iterator
                                        it=mapOfDirects.find(direction);
           it->choosenDirection(*this);                     // any of this syntax work
         //  (*it)->choosenDirection(*this);              //
         //  *it->choosenDirection(*this);                //
 

error::<const sf::Keyboard::Key, std::shared_ptr<DirectConsition>> has no member 'choosenDirection' "

but this syntax works mapOfDirects[direction]->choosenDirection(*this);
I don't know how to do it ;/
Could you help me please ? Thanks advance !


EDIT
It works : it->second->choosenDirection(*this)  :D
« Last Edit: September 24, 2016, 11:02:36 am by Klemens »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: sf::Keyboard
« Reply #3 on: September 24, 2016, 11:32:22 am »
Yes, sorry for the wrong syntax.

And you should really use the 'auto" keyword here ;)
Laurent Gomila - SFML developer