Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: sf::Keyboard::isKeyPressed() stops working after a few times. [sfml 2.1]  (Read 3159 times)

0 Members and 1 Guest are viewing this topic.

Luapina

  • Newbie
  • *
  • Posts: 7
    • View Profile
Hey guys,

I have the following Code in my main loop
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z) && ChernarusSprite.getScale().x <= 0.95f)
                {
                        ChernarusSprite.setScale(ChernarusSprite.getScale().x + 0.05f, ChernarusSprite.getScale().y + 0.05f);                  
                }

It allways stopped working after a few times, so I decided to check wether the Keys are still recognized like this:
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
                        std::cout << "Z" << std::endl;
And it doesnt get recognized. Any Ideas why this part of SFML 2.1 stops working?


int main()
{
        sf::RenderWindow window(sf::VideoMode(1200, 900), "asdasfasfasd", sf::Style::Close | sf::Style::Resize);
        window.setFramerateLimit(60);
        tgui::Gui gui(window);
        sf::RectangleShape MapBG;
        sf::Image Chernarus;
                Chernarus.loadFromFile("chernarus_8000x7999px.jpg");
        sf::Texture ChernarusTexture;
                ChernarusTexture.loadFromImage(Chernarus);
                ChernarusTexture.setSmooth(true);
                sf::Sprite ChernarusSprite(ChernarusTexture);
                ChernarusSprite.scale(sf::Vector2f(0.1f, 0.1f));
                ChernarusSprite.setPosition(12, 12);

        MapBG.setPosition(sf::Vector2f(12, 12));
        MapBG.setSize(sf::Vector2f(800, 800));
        MapBG.setOutlineThickness(2.0f);
        MapBG.setOutlineColor(sf::Color(80, 80, 80));
        MapBG.setFillColor(sf::Color(0, 0, 0));

        if (gui.setGlobalFont("fonts/DejaVuSans.ttf") == false)
                return 1;

        loadWidgets(gui);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                                window.close();

                        gui.handleEvent(event);
                }

                tgui::Callback callback;
                while (gui.pollCallback(callback))
                {
                        switch (callback.id)
                        {
                                case 1:
                                        window.setTitle(gen_random(32));
                                break;
                                case 2:
                                       
                                break;
                                case 3:
                               
                                break;
                                default:

                                break;
                        }
                }



                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z) && ChernarusSprite.getScale().x <= 0.95f)
                {
                        ChernarusSprite.setScale(ChernarusSprite.getScale().x + 0.05f, ChernarusSprite.getScale().y + 0.05f);                  
                }
               
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
                        std::cout << "Z" << std::endl;

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::H) && ChernarusSprite.getScale().x >= 0.15f)
                {
                        ChernarusSprite.setScale(ChernarusSprite.getScale().x - 0.05f, ChernarusSprite.getScale().y - 0.05f);
                       
                }
                       
                if (sf::Keyboard::isKeyPressed(sf::Keyboard::H))
                        std::cout << "H" << std::endl;

                window.clear(sf::Color(245, 245, 255));
                gui.draw();
                window.draw(MapBG);
                window.draw(ChernarusSprite);
                window.display();
        }
        return 0;
}

Dont now, wether this is important, but im using tgui for GUI

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Please try to reproduce it in a complete and minimal example, as explained here.
Laurent Gomila - SFML developer

Dark Goomba

  • Newbie
  • *
  • Posts: 9
    • View Profile
It looks like it should work but you could try putting the real time keyboard event in the poll event loop:
while (Window.pollEvent(Event))
{
     if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
          std::cout << "Z" << std::endl;
}
Goombas are mushrooms.

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
It looks like it should work but you could try putting the real time keyboard event in the poll event loop: [snip]

No! That's just wrong.  ;)

What this does is, for each event (mouse moved, key pressed, window resized, ...) it check if Z is pressed. So a) it does test it too often to make sense b) it won't test it if nothing happen (well, that's not that bad but it's not logical nevertheless).
SFML / OS X developer

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Are there any other parts of your program that could affect your input? I tested SFML handling multiple if statements of the same condition and my input works fine e.g.

 if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
{
        std::cout << "A" << std::endl;          
}
       
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Z))
        std::cout << "Z" << std::endl;

I think it may be your gui hanging rather than SFML, could it be
gui.handleEvent(event);
is maybe doing something you do not want it to do?

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Remember that there's a difference between events and realtime input.
With events you get a notification (an event) when something happens (like a key press or key release) and reading that event from the queue consumes it (you won't get it twice).
With realtime events (like sf::Keyboard::isKeyPressed) you are effectively asking "is this key pressed right this very millisecond" and the answer you get will depend on the actual state of the key at the precise time you ask (and if you ask multiple times you'll get the same answer if the state has not changed; or different answers if state has changed).
It's usually not wise to mix events and realtime input. You can do it but it's usually a lot less confusing to stick to one or the other (and usually whatever you are doing mandates one or the other).
Not specifically related to your question, just wanted to point it out since it seemed semi-relevant.