SFML community forums

Help => Window => Topic started by: bobtran12 on December 22, 2014, 12:09:57 am

Title: Why is this code detecting the key twice?
Post by: bobtran12 on December 22, 2014, 12:09:57 am
Every time I hit the "x" key, it shows two outputs instead of one.

Code:
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
        sf::Vector2f screenDimensions(800, 600);
        sf::RenderWindow Window;
        Window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "Test", sf::Style::Titlebar | sf::Style::Close);

        Window.setKeyRepeatEnabled(false);

        int i = 0;

        while(Window.isOpen())
        {
                sf::Event Event;
                while(Window.pollEvent(Event))
                {
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::X))
                        {
                                std::cout << i << std::endl;
                                i++;
                        }
                }
        }
}

Output (when pressing "x" key once):
0
1

How do I fix it so that it only detects and outputs one?

EDIT: I'm on Windows 7 and using SFML 2.1.
Title: Re: Why is this code detecting the key twice?
Post by: Ixrec on December 22, 2014, 12:20:23 am
You probably want events, not real-time input.  You should read the tutorials on both input methods so you understand the difference and why you want events in this case.

http://www.sfml-dev.org/tutorials/2.2/window-events.php
http://www.sfml-dev.org/tutorials/2.2/window-inputs.php

Incidentally, your current code is mixing real-time input with event-based input, which is always a really bad idea, as this example proves.  In this case, I believe you're getting exactly two outputs because isKeyPressed is being called after the KeyPressed event and the KeyReleased event.  Any other event (say, moving the mouse) would probably cause it to output some more.
Title: Re: Why is this code detecting the key twice?
Post by: bobtran12 on December 22, 2014, 12:41:23 am
You probably want events, not real-time input.  You should read the tutorials on both input methods so you understand the difference and why you want events in this case.

http://www.sfml-dev.org/tutorials/2.2/window-events.php
http://www.sfml-dev.org/tutorials/2.2/window-inputs.php

Incidentally, your current code is mixing real-time input with event-based input, which is always a really bad idea, as this example proves.  In this case, I believe you're getting exactly two outputs because isKeyPressed is being called after the KeyPressed event and the KeyReleased event.  Any other event (say, moving the mouse) would probably cause it to output some more.

Got it. Thanks.

So, would something like this have any problems?
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
{
        sf::RenderWindow Window;
        Window.create(sf::VideoMode(800, 600), "Test", sf::Style::Titlebar | sf::Style::Close);

        Window.setKeyRepeatEnabled(false);

        int i = 0;

        while(Window.isOpen())
        {
                sf::Event Event;
                while(Window.pollEvent(Event))
                {
                        if (Event.type == sf::Event::KeyPressed)
                        {
                                if (Event.key.code == sf::Keyboard::X)
                                {
                                        std::cout << i << std::endl;
                                        i++;
                                }
                        }
                }
        }
}
Title: Re: Why is this code detecting the key twice?
Post by: Ixrec on December 22, 2014, 01:03:12 am
>So, would something like this have any problems?

That code looks perfectly fine to me.
Title: Re: Why is this code detecting the key twice?
Post by: Hapax on December 22, 2014, 03:05:33 am
Could do with being able to close the window, though...