c++c++17sfmlcodelite

sf::Event::Keypressed not working inside of poll event loop -SFML-2.5.1


#include <iostream>
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <SFML/System.hpp>

int main(){
    
    sf::RenderWindow window(sf::VideoMode(640, 480),  "Window");    

       while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                switch (event.type)
                {
                    case sf::Event::Closed:
                        window.close(); break;
                    
                    case sf::Event::KeyPressed:
                        std::cout << event.key.code;
                        break;
                    
                }
            }
         }
 return 0;
}

I get a bunch of warnings like this enumeration value 'MouseLeft' not handled in switch

I tried putting it in an if statement instead of a switch that got rid of the warnings but it still will not output event.key.code i could only get it to work when I put it outside of the pollevent(event) loop but then letters werent picked up.


Solution

  • Add an << std::flush; after your couts and you will see the key codes. Otherwise they will not be printed until SFML main loop is left (not when interrupted but when closed gracefully using x button).