Hello,
I want to rely on the support for event repetition. However, it isn't behaving the way I was expecting. Consider the following program:
#include <SFML/Graphics.hpp>
#include <iostream>
auto main() -> int {
sf::RenderWindow win(sf::VideoMode(100, 100), "KeyTest");
win.setFramerateLimit(8);
win.setKeyRepeatEnabled(true);
while (win.isOpen()) {
// consume the events
for (sf::Event event; win.pollEvent(event);)
{
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::A)
std::cout << "A";
else if (event.key.code == sf::Keyboard::B)
std::cout << "B";
} else if (event.type == sf::Event::Closed)
return 0;
}
std::cout << std::flush;
} // while
}
With this code, if I press A, then "A" is displayed. If I now press B while still holding A pressed, then "B" is displayed but "A" is no longer displayed (the KeyPressed event for A is no longer generated even though A is still pressed). Finally, if I release B while still holding A pressed, then nothing is printed (no repeated event KeyPressed is generated for A, but is pressed). However, if I released A instead of B, then "B" would have still been printed (the KeyReleased event for A does not affect the repetition of the KeyPress event for B).
My guess is that an event is only repeated if this potential event to be repeated does exactly match the last event that was generated with the same type (i.e., same value in the type data member of the sf::Event).
no, the problem is the line else if (event.key.code == sf::Keyboard::B)
by using "else if", this code is going to be executed only if A is NOT pressed. if you want both to be checked, just remove the "else"
no, the problem is the line else if (event.key.code == sf::Keyboard::B)
by using "else if", this code is going to be executed only if A is NOT pressed. if you want both to be checked, just remove the "else"
I don't think that's the issue. I'm handling a single event per iteration of the for loop, i.e., I'm only pulling one event from the event queue at a time with sf::Window::pollEvent() and there can't be an event whose code member is both A and B at the same time.