SFML community forums

Help => Window => Topic started by: Fudance on January 18, 2009, 01:32:33 am

Title: Multiple keys pressed
Post by: Fudance on January 18, 2009, 01:32:33 am
Hi,

I've just discovered SFML and am doing a little experiment game while I get my head around the main principles of the library. So far its been a blast but I've come unstuck when trying to test for multiple keypresses when using the GetInput member function. I'm trying to make a character move diagonally when 2 arrow keys (i.e. a user presses both down and left) are pressed but for some reason it doesn't work. This is what I'm trying to do:-

Code: [Select]

if....
...
...
else if (App.GetInput().IsKeyDown(sf::Key::Down) && App.GetInput().IsKeyDown(sf::Key::Left)) {
                       PlayerSprite.Move(-100 * ElapsedTime,  100 * ElapsedTime);
                }


I'm sure I'm doing something stupid here, but I'm pretty new to this and need it pointing out  :)
Title: Multiple keys pressed
Post by: Laurent on January 18, 2009, 11:59:31 am
What does "it doesn't work" mean exactly?
Title: Multiple keys pressed
Post by: Fudance on January 18, 2009, 04:49:08 pm
Sorry, perhaps I should have elaborated further.

The code snippet below is pretty much ripped from the tutorials on the wiki. I test if the up, down, left and right keys are pressed in the 'if... else if' block and move the character appropriately. This works fine for movement where a single key is held down, so I'm trying to improve this further by adding the ability for the user to hold down multiple arrow keys at the same time to get diagonal movement.

At the moment I have a single 'else if' block to test if the user held down both the down and left arrow keys (the code I pasted in the OP).  I don't think this test is working how I think it should, it seems that my test to check for whether both the keys are down is always false.

Does that make more sense?
Title: Re: Multiple keys pressed
Post by: dewyatt on January 21, 2009, 01:18:53 pm
Post more of the code if you can.
If I had to guess I'd say you're making a simple logic mistake.
Step through with a debugger if you can.
Title: Multiple keys pressed
Post by: Daazku on January 21, 2009, 04:59:02 pm
Try to use boolean for the pressed key

Code: [Select]

bool         LeftKeyDown     = Input.IsKeyDown(sf::Key::Left);


And after just do something like that:

(This exemple change de value of x)
Code: [Select]

if (LeftKeyDown)
{
x -= 1;
}

if (RightKeyDown)
{
x += 1;
}

Player.move(x * elapsedtime(), y);
Title: Multiple keys pressed
Post by: Fudance on January 21, 2009, 11:16:23 pm
OK, I will try that, it never occured to try assigning the pressed key to a bool. If it doesn't work I'll post more of the code.

Thanks for the suggestions.
Title: Multiple keys pressed
Post by: Fudance on January 22, 2009, 12:02:40 am
Aha, dewyatt was correct, the problem was simple. I implemented the comparisons in an if-else block which meant if I successfully found a pressed key it would not cascade down and check for other keys being pressed as well. Changing everything to have its own if means every combination is now checked for.

D'oh, so simple. Implementing the bool suggestion makes the code more readble so that was worthwhile as well. Thanks for all the help, I'm sure I'll need more later!