Search code examples
c++sfml

Mouse and keyboard always get input althoughtI don't do anything


I'm coding a game with SFML library. I have some code to do when I press key and mouse click

I use this: Sf::keyBoard::isKeyPressed And this: Sf::Mouse::isButtonPressed

But somehow there code always run every frame although I don't click or press anything.

Is my computer wrong or something else?


Solution

  • Can you try like this?

        while (window.isOpen())
        {
            sf::Event event;
    
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                    window.close();
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
                {
                    _isGameNotStarted = false;
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                    p->playerMoveR(_isGameNotStarted);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                    p->playerMoveL(_isGameNotStarted);
                }
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::R))
                {
                    if (_isGameFinished == true)
                        resetLevel();
                }
            }
        }