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

Author Topic: Getting around automatic key-pauses?  (Read 2458 times)

0 Members and 1 Guest are viewing this topic.

pabloist

  • Newbie
  • *
  • Posts: 26
    • View Profile
Getting around automatic key-pauses?
« on: October 20, 2010, 01:47:23 am »
When a key is pressed, if it is held down, it takes a bit for it to continue registering. I'm pretty sure there's a way around this, but I can't figure it out.

Also, how can I handle multiple key inputs at the same time? If say, I'm holding the Right key down, then press space, the Right key is no longer being held down (and I have to press it again).

Code: [Select]
while (App.IsOpened())
        {
            sf::Event Event;

            const sf::Input &Input = App.GetInput();

            while (App.GetEvent(Event))
            {
                if (Event.Type == sf::Event::Closed)
                    App.Close();
                if (Input.IsKeyDown(sf::Key::Left))
                    Game.player->Move("Left");
                else if (Input.IsKeyDown(sf::Key::Right))
                    Game.player->Move("Right");

                if (Event.Type == sf::Event::KeyPressed)
                {
                    if (Event.Key.Code == sf::Key::Escape)
                        App.Close();
                    else if (Event.Key.Code == sf::Key::Space)
                        Game.pl_lasers.push_back(Game.player->FireLaser());
                }
            }


About the Input.IsKeyDown() being mixed up with the Event.Type checking, I think I remember reading something about Input being better for some reason.. tbh, I don't remember why I used it.

Thanks for help!

Walker

  • Full Member
  • ***
  • Posts: 181
    • View Profile
Getting around automatic key-pauses?
« Reply #1 on: October 20, 2010, 03:05:36 am »
Key repeat/pause stuff is set in your OS. sf::Input is good because it isn't restrained by that.

Generally you will want to use sf::Input for most game input. Only use events for non keyboard/mouse events like close.

Quote
Also, how can I handle multiple key inputs at the same time?

Do you mean with sf::Input? I've never had a problem with this. Code sample?

pabloist

  • Newbie
  • *
  • Posts: 26
    • View Profile
Getting around automatic key-pauses?
« Reply #2 on: October 20, 2010, 04:26:02 am »
Even using sf::Input, I'm getting the key delay. Same with the problem of 2 keypresses - perhaps I'm using it wrong?

(This IS different from first code posted, first code was mixing sf::Event and sf::Input stuff)

Code: [Select]
while (App.IsOpened())
{
    sf::Event Event;

    const sf::Input &Input = App.GetInput();

    if (Game.enemies.empty())
        Game.SpawnEnemies(sf::Randomizer::Random(10,50));

    while (App.GetEvent(Event))
    {
        if (Input.IsKeyDown(sf::Key::Left))
            Game.player->Move("Left");
       
        if (Input.IsKeyDown(sf::Key::Right))
            Game.player->Move("Right");                    
       
        if (Input.IsKeyDown(sf::Key::Space))
            Game.pl_lasers.push_back(Game.player->FireLaser());

        if (Input.IsKeyDown(sf::Key::Escape))
            App.Close();
       
        if (Event.Type == sf::Event::Closed)
            App.Close();
    }
    Game.Update();
    // more stuff....
}

AdrianC

  • Newbie
  • *
  • Posts: 5
    • MSN Messenger - comiseladrian@yahoo.com
    • AOL Instant Messenger - 2914+Trelle+Way+
    • View Profile
Getting around automatic key-pauses?
« Reply #3 on: October 20, 2010, 04:48:23 am »
You don't have to put
Code: [Select]
if (Input.IsKeyDown(sf::Key::Left))
Game.player->Move("Left");

within the Get Event loop. You can use it outside of that loop. I imagine that would fix your problem.

pabloist

  • Newbie
  • *
  • Posts: 26
    • View Profile
Getting around automatic key-pauses?
« Reply #4 on: October 20, 2010, 05:32:37 am »
Oh, awesome! Thanks :)