Hi, so I made a std::unordered_map to contain my user input key bindings. I have a struct called "Bindings" that contains the following members.
struct Binding
{
Binding() {}
Binding(InputType inputTypes, sf::Event::EventType eventType1, sf::Event::EventType eventType2,
sf::Keyboard::Key keyCode1, sf::Keyboard::Key keyCode2,
sf::Mouse::Button mouseCode1, sf::Mouse::Button mouseCode2)
: my_InputTypes{ inputTypes }, m_enumEventType1 {eventType1}, m_enumEventType2 { eventType2 },
m_keyCode1{ keyCode1 }, m_keyCode2{ keyCode2 }, m_mouseCode1{ mouseCode1 },
m_mouseCode2{ mouseCode2 }{}
InputType my_InputTypes;
sf::Event::EventType m_enumEventType1;
sf::Event::EventType m_enumEventType2;
sf::Keyboard::Key m_keyCode1;
sf::Keyboard::Key m_keyCode2;
sf::Mouse::Button m_mouseCode1;
sf::Mouse::Button m_mouseCode2;
};
using BindingMap = std::unordered_map<std::string, Binding >;
I read a .csv file to enter my bindings.
void EventManager::LoadBindings()
{
std::ifstream keyBindings("Keys.csv");
if (!keyBindings.is_open()) { std::cout << "Error loading Keys.csv..."; }
int number_of_lines = 0;
std::string lineCount;
while (std::getline(keyBindings, lineCount))
++number_of_lines;
std::cout << "Number of lines in .csv file: " << number_of_lines << '\n';
keyBindings.close();
keyBindings.open("Keys.csv");
std::string mapKey;
std::string myInputType;
std::string eventType1;
std::string eventType2;
std::string keyCode1;
std::string keyCode2;
std::string mouseCode1;
std::string mouseCode2;
for (int i=0; i <= (number_of_lines-1); ++i)
{
std::getline(keyBindings, mapKey,',');
std::getline(keyBindings, myInputType,',');
std::getline(keyBindings, eventType1,',');
std::getline(keyBindings, eventType2, ',');
std::getline(keyBindings, keyCode1,',');
std::getline(keyBindings, keyCode2,',');
std::getline(keyBindings, mouseCode1,',');
std::getline(keyBindings, mouseCode2,'\n');
std::cout << mapKey << "|"
<< stoi(myInputType) << "|"
<< stoi(eventType1) << "|"
<< stoi(eventType2) << "|"
<< stoi(keyCode1) << "|"
<< stoi(keyCode2) << "|"
<< stoi(mouseCode1) << "|"
<< stoi(mouseCode2) << '\n';
m_bindingMap.insert(std::pair <std::string, Binding>
(mapKey, { static_cast<InputType>(stoi(myInputType)),
static_cast<sf::Event::EventType>(stoi(eventType1)),
static_cast<sf::Event::EventType>(stoi(eventType2)),
static_cast<sf::Keyboard::Key>(stoi(keyCode1)),
static_cast<sf::Keyboard::Key>(stoi(keyCode2)),
static_cast<sf::Mouse::Button>(stoi(mouseCode1)),
static_cast<sf::Mouse::Button>(stoi(mouseCode2))}));
}
keyBindings.close();
PrintMap();
}
void EventManager::PrintMap()
{
for (auto element : m_bindingMap)
{
std::cout << element.first << " :: " << static_cast<int>(element.second.my_InputTypes)
<< " :: " << static_cast<int>(element.second.m_enumEventType1)
<< " :: " << static_cast<int>(element.second.m_enumEventType1)
<< " :: " << static_cast<int>(element.second.m_keyCode1)
<< " :: " << static_cast<int>(element.second.m_keyCode2)
<< " :: " << static_cast<int>(element.second.m_mouseCode1)
<< " :: " << static_cast<int>(element.second.m_mouseCode2)
<< std::endl;
}
}
The map works perfectly and is tested... Now, I'm trying to make a multikey bool that flags "true" if 2 or more buttons are pressed at the same time.
bool EventManager::TestEvent(Binding binding, sf::Event event)
{
//MultiKeyEvents
if ((binding.my_InputTypes == InputType::MultikeyEvent) &&
(binding.m_enumEventType1 == event.KeyPressed) &&
(binding.m_enumEventType2 == event.MouseButtonPressed) &&
(binding.m_keyCode1 == event.key.code) &&
(binding.m_mouseCode1 == event.mouseButton.button))
{
return (true);
}
return (false);
}
For this example, the keycode = 0 for "A" and the mouse button code = 0 for "Left Mouse Button". I'm trying to register "true" when they are both pressed together. However, with this if statement it will register "true" if I press "A" or the "Left Mouse Button" separately. What am I missing here!? I only want it to be "true" if they are both pressed at the same time.