I'm trying to write some unit test methods. I want to test method which checks if key is pressed or released. And here is my question:
Is it possible in SFML with C++ to simulate random key press on keyboard?
Or I will just have to trust myself that this works?
The internals of sf::Keyboard::isKeyPressed would make simulation difficult, but if you are looking to simulate KeyPressed or KeyReleased events I'm pretty sure the following should work:
const sf::Event simulateKeypress(sf::Keyboard::Key key, bool alt, bool control, bool shift, bool system)
{
sf::Event::KeyEvent data;
data.code = key;
data.alt = alt;
data.control = control;
data.shift = shift;
data.system = system;
sf::Event event;
event.type = sf::Event::KeyPressed;
event.key = data;
return event;
}
//your handler here
handleEvent(simulateKeypress(sf::Keyboard::Key::A, false, false, false, false));
I'm unable to test this at the moment... If it works, then you should be able to make similar functions for other events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With