SFML community forums

Help => Window => Topic started by: BeautiCode on June 29, 2014, 03:28:06 am

Title: See if a user is holding a key?
Post by: BeautiCode on June 29, 2014, 03:28:06 am
How do I tell whether the user is holding a key down, in the normal event handling loop?
I just use a switch/case statement and do an action based upon if the key is pressed. But I wanna repeatedly do that action as long as the key is being held down. Could someone provide an example?
Title: Re: See if a user is holding a key?
Post by: BeautiCode on June 29, 2014, 03:45:37 am
Well nevermind I noticed that it automatically does that. But what about if the user is holding down 2 keys at once?
Title: Re: See if a user is holding a key?
Post by: G. on June 29, 2014, 04:46:37 am
You can use sf::Keyboard::IsKeyPressed (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Keyboard.php#a80a04b2f53005886957f49eee3531599). It returns true as long as the specified key is held down.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
...
}
Title: Re: See if a user is holding a key?
Post by: BeautiCode on June 29, 2014, 04:54:48 am
You can use sf::Keyboard::IsKeyPressed (http://www.sfml-dev.org/documentation/2.0/classsf_1_1Keyboard.php#a80a04b2f53005886957f49eee3531599). It returns true as long as the specified key is held down.
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
...
}
Thanks