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

Author Topic: Multiple keys pressed  (Read 12697 times)

0 Members and 1 Guest are viewing this topic.

Fudance

  • Newbie
  • *
  • Posts: 4
    • View Profile
Multiple keys pressed
« 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  :)

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Multiple keys pressed
« Reply #1 on: January 18, 2009, 11:59:31 am »
What does "it doesn't work" mean exactly?
Laurent Gomila - SFML developer

Fudance

  • Newbie
  • *
  • Posts: 4
    • View Profile
Multiple keys pressed
« Reply #2 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?

dewyatt

  • Jr. Member
  • **
  • Posts: 75
    • View Profile
    • http://dewyatt.blogspot.com
Re: Multiple keys pressed
« Reply #3 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.

Daazku

  • Hero Member
  • *****
  • Posts: 896
    • View Profile
Multiple keys pressed
« Reply #4 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);
Pensez à mettre le tag [Résolu] une fois la réponse à votre question trouvée.
Remember to add the tag [Solved] when you got an answer to your question.

Fudance

  • Newbie
  • *
  • Posts: 4
    • View Profile
Multiple keys pressed
« Reply #5 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.

Fudance

  • Newbie
  • *
  • Posts: 4
    • View Profile
Multiple keys pressed
« Reply #6 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!

 

anything