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

Author Topic: InputManager(single KeyPress)  (Read 4887 times)

0 Members and 2 Guests are viewing this topic.

eliburgi

  • Newbie
  • *
  • Posts: 6
    • View Profile
InputManager(single KeyPress)
« 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 ;)

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: InputManager(single KeyPress)
« Reply #1 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.

eliburgi

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: InputManager(single KeyPress)
« Reply #2 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;
}

eliburgi

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: InputManager(single KeyPress)
« Reply #3 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);
         }
      }

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: InputManager(single KeyPress)
« Reply #4 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();
Failing to succeed does not mean failing to progress!

eliburgi

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: InputManager(single KeyPress)
« Reply #5 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 ;)

Geheim

  • Full Member
  • ***
  • Posts: 201
    • View Profile
    • Email
Re: InputManager(single KeyPress)
« Reply #6 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 first. ;)
Failing to succeed does not mean failing to progress!

eliburgi

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: InputManager(single KeyPress)
« Reply #7 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 ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: InputManager(single KeyPress)
« Reply #8 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. But it's also a good idea to do it once yourself, in order to learn the principles.
« Last Edit: February 26, 2014, 08:15:49 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

eliburgi

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: InputManager(single KeyPress)
« Reply #9 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 ;)

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: InputManager(single KeyPress)
« Reply #10 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();
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: