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

Author Topic: Why is this code detecting the key twice?  (Read 4053 times)

0 Members and 1 Guest are viewing this topic.

bobtran12

  • Guest
Why is this code detecting the key twice?
« 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.
« Last Edit: December 22, 2014, 12:13:40 am by bobtran12 »

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Why is this code detecting the key twice?
« Reply #1 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.
« Last Edit: December 22, 2014, 12:24:55 am by Ixrec »

bobtran12

  • Guest
Re: Why is this code detecting the key twice?
« Reply #2 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++;
                                }
                        }
                }
        }
}

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Why is this code detecting the key twice?
« Reply #3 on: December 22, 2014, 01:03:12 am »
>So, would something like this have any problems?

That code looks perfectly fine to me.

Hapax

  • Hero Member
  • *****
  • Posts: 3346
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Why is this code detecting the key twice?
« Reply #4 on: December 22, 2014, 03:05:33 am »
Could do with being able to close the window, though...
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything