SFML community forums
Help => General => Topic started by: pabloist 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).
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!
-
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.
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?
-
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)
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....
}
-
You don't have to put
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.
-
Oh, awesome! Thanks :)