SFML community forums

Help => General => Topic started by: eliburgi on February 26, 2014, 07:13:01 pm

Title: InputManager(single KeyPress)
Post by: eliburgi on February 26, 2014, 07:13:01 pm
Hello guys,
I am new to SFML, but have some experience in C++. I am not new to Game programming, because I have already created some games with C# and Java.
So my question is: How do I detect a single Key press in SFML?
I want to create a class InputManager, and I already read about Events and Real-Time Input.
Sry for my english, i am from Austria ;)
Title: Re: InputManager(single KeyPress)
Post by: Jesper Juhl on February 26, 2014, 07:15:57 pm
Poll for events as part of your game loop and react to the KeyPressed event(s).
The tutorials have examples.
Title: Re: InputManager(single KeyPress)
Post by: eliburgi on February 26, 2014, 07:18:48 pm
Ok thanks, but heres some example:

void InputManager::Update(sf::Event event)
{
   mEvent = event;
}

bool InputManager::isKeyPressed(sf::Keyboard::Key key)
{
   if (mEvent.key.code == key)
      return true;

   return false;
}
Title: Re: InputManager(single KeyPress)
Post by: eliburgi on February 26, 2014, 07:19:38 pm
int the main:
while (mainWindow->pollEvent(event))
      {
         switch (event.type)
         {
            case sf::Event::Closed:
               mainWindow->close();
               break;

            case sf::Event::KeyPressed:
               InputManager::Update(event);
         }
      }
Title: Re: InputManager(single KeyPress)
Post by: Geheim on February 26, 2014, 07:30:01 pm
So my question is: How do I detect a single Key press in SFML?

You already implemented this case:
while(mainWindow->pollEvent(event))
{
    switch (event.type)
    {
        case sf::Event::KeyPressed:  // Here you detect if you press a key
           ...
    }
}

To get the actual key you say: event.key.code, so for example
if(event.key.code == sf::Keyboard::Escape)
   mainWindow.close();
Title: Re: InputManager(single KeyPress)
Post by: eliburgi on February 26, 2014, 07:33:42 pm
Ok thanks that already helped a lot, but how do I transfer that to my class?
Because I want to be able to call InputManager::isKeyPressed(//key) in the whole game ;)
Title: Re: InputManager(single KeyPress)
Post by: Geheim on February 26, 2014, 07:40:14 pm
Then you should use realtime input, because events (in your case sf::Event::KeyPressed) only happen under certain circumstances (in your case only once when a key is pressed).

You should read the documentation (http://www.sfml-dev.org/documentation/2.1/classsf_1_1Keyboard.php#a80a04b2f53005886957f49eee3531599) first. ;)
Title: Re: InputManager(single KeyPress)
Post by: eliburgi on February 26, 2014, 08:04:17 pm
No you understood me false ;)
I know what Real-Time Input(sf::Keyboard) is, and at least something about events but if I want to detect a single Key press (for example: pressing the Space bar to jump) everywhere in the programm, so my idea was to create a class InputManager. Because so far I understood it, with RealTime Input I only can detect if a key is down but I can´t detect a single Key press with it
But thanks for your patient and help ;)
Title: Re: InputManager(single KeyPress)
Post by: Nexus on February 26, 2014, 08:14:03 pm
Because so far I understood it, with RealTime Input I only can detect if a key is down but I can´t detect a single Key press with it
Yes, that's exactly why you need events.

You don't want to handle SFML events "everywhere in the program". It's not a good idea to do that with real-time input either, despite the global nature of the sf::Keyboard class. Inside the game, you should have exactly one class that is responsible for input handling, and that translates raw input (such as "Space key pressed") into high-level actions (such as "Jump"). These actions are then disclosed to their recipients, mostly in the form of a method call like player.jump();

If you need an automated way to do that, you can have a look at Thor.Input (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-actions.html). But it's also a good idea to do it once yourself, in order to learn the principles.
Title: Re: InputManager(single KeyPress)
Post by: eliburgi on February 26, 2014, 08:18:49 pm
Ahh ok that makes things clear ;)
Maybe I should read the SFML book, but partly the structure is very complex(commands), even if the princip behind that is genius, I like the traditional way ;)
Title: Re: InputManager(single KeyPress)
Post by: Nexus on February 26, 2014, 09:29:39 pm
Yes, don't hesitate to experiment with different approaches. The command system from the book is just one possible way of doing it, it also has its drawbacks.

But in any case, I would think about a design that separates input handing and game logic. At least if you plan to make things scalable (e.g. allow key rebinding), you should avoid code like
if (/* Space key pressed */)
    player.jump();