Hello, this is my first post here, so I hope I am able to format everything correctly.
I am looking to initialize a container that holds pairs of sf::Keyboard:Key and bool, which is intended to track the state of each key pressed using the event loop. I am doing this in order to allow for checking if multiple key combinations are pressed (for example, Ctrl-F is pressed), in an InputManager class.
I saw this post:
https://stackoverflow.com/questions/17888993/key-repetition-in-sfml-2-0which has a great idea to initialize an std::array<bool, sf::Keyboard::KeyCount> and then fill it with false.
However, I can not determine how to do this within a class initialization / method. I want the InputManager to have a private member variable like this, that I fill with false. Ideally, this class would have only static members to make it easier to use throughout the application.
I tried the following:
class InputManager {
private:
static std::array<bool, sf::Keyboard::KeyCount> keyState;
public:
static void initializeKeyStates();
};
However, when I try to use "keyState", it complains about incomplete type. For example, in InputManager.cpp, I have:
void InputManager::initializeKeyStates()
{
keyState.fill(false); // Error here, incomplete type is not allowed
}
I realize this may be more of a "std::array" question. But, I also considered using an std::map<sf::Keyboard::Key, bool>, but then actually inserting every sf::Keyboard::Key becomes very tedious -- I can't determine how to insert the pairs via a loop.
Any help on this is greatly appreciated -- having this datastructure in my InputManager will make everything much easier for my game!
Please let me know if I'm missing anything important in this post
EDIT: I want to also clarify a few things in the above post --
1. Using sf::Keyboard::isKeyPressed() is not appropriate for my goal, as I have items that I want to check for using events so that it only happens once (I have window.setKeyRepeatEnabled(false)).
2. The first usage of this (as a proof of concept) will be toggling an FPS display. I have that working currently via the standard event loop, like this:
bool control_pressed = false;
while (window.isOpen()) {
window.clear();
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
if (event.type == sf::Event::KeyPressed) {
if (event.key.code == sf::Keyboard::F) {
if (control_pressed) {
fpsDisplayText.shouldToggle = true;
}
} else if (event.key.code == sf::Keyboard::LControl) {
control_pressed = true;
}
}
if (event.type == sf::Event::KeyReleased) {
if (event.key.code == sf::Keyboard::LControl) {
control_pressed = false;
}
}
}
fpsDisplayText.checkToggle(); // this will determine if "shouldToggle" and then toggle
// and set "shouldToggle" to false if it toggled
.
.
...