Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SFML/C++ Simulating user input

Tags:

c++

sfml


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?

like image 709
Jakub Stasiak Avatar asked Feb 24 '26 20:02

Jakub Stasiak


1 Answers

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.

like image 119
Conduit Avatar answered Feb 26 '26 11:02

Conduit