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

Author Topic: Understanding sf::Event::KeyPressed  (Read 4133 times)

0 Members and 1 Guest are viewing this topic.

Avency

  • Full Member
  • ***
  • Posts: 113
    • View Profile
Understanding sf::Event::KeyPressed
« on: February 14, 2008, 08:15:14 pm »
In each loop I receive a sf::Event::KeyPressed event , if a key is held down.
Is there a way to receive a single key press?
If I wanted to check if a key is beeing held down, I would use the sf::Input class instead.
sf::Event::KeyReleased events are only received if a key is released, as I would expect it.

Here is a short example of what I mean:

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>

int main()
{

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "Events");
   
    sf::Font Font;
    Font.LoadFromFile("ARIAL.TTF", 40);
   
    sf::String Text("", Font, 32);
   
    std::ostringstream oss;
   
    int NbKeysPressed = 0; //counts, how often a key has been pressed

    // Start game loop
    bool Running = true;
    while (Running)
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            switch(Event.Type)
            {
            case sf::Event::Closed:
            {
            Running = false;
            break;
            }
            case sf::Event::KeyPressed:
            {
            ++NbKeysPressed;
           
            oss.str("");
            oss << NbKeysPressed;
            Text.SetText(oss.str());
           
            break;
            }
            case sf::Event::KeyReleased:
            {
            --NbKeysPressed;
            oss.str("");
            oss << NbKeysPressed;
            Text.SetText(oss.str());

                  break;
            }
            default:
            {
            break;
            }      
            }
        }
       
        App.Draw(Text);

        App.Display();
    }

    return EXIT_SUCCESS;
}


Any ideas?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Understanding sf::Event::KeyPressed
« Reply #1 on: February 15, 2008, 02:14:03 am »
This is just to reflect the OS default behaviour (at least on Windows). But I'm going to add a sf::Window::SetKeyAutoRepeat(bool) function to control that ;)
Laurent Gomila - SFML developer