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

Author Topic: two keyboard keys pressed at the same time?  (Read 5859 times)

0 Members and 1 Guest are viewing this topic.

guysudai1

  • Newbie
  • *
  • Posts: 1
    • View Profile
two keyboard keys pressed at the same time?
« on: July 22, 2017, 04:46:31 pm »
How do I make my "game" take 2 key inputs, for example if the user clicks w and d he moves up right.

here's my current code:

while (window.isOpen()) {
                Event eventCheck;
                while (window.pollEvent(eventCheck)) {
                        switch (eventCheck.type) {
                        case Event::Closed:
                                window.close();
                                break;
                        case Event::KeyPressed:
                                switch (eventCheck.key.code) {
                                        case Keyboard::W:
                                                if (Keyboard::isKeyPressed(Keyboard::A)) {
                                                        const Vector2f spritePos = sprite.getPosition();
                                                        sprite.setPosition(spritePos.x, spritePos.y - 5);
                                                }
                                                break;
                                        case Keyboard::A:
                                                if (Keyboard::isKeyPressed(Keyboard::A)) {
                                                        const Vector2f spritePos = sprite.getPosition();
                                                        sprite.setPosition(spritePos.x - 5, spritePos.y);
                                                }
                                                break;
                                        case Keyboard::S:
                                                if (Keyboard::isKeyPressed(Keyboard::S)) {
                                                        const Vector2f spritePos = sprite.getPosition();
                                                        sprite.setPosition(spritePos.x, spritePos.y + 5);
                                                }
                                                break;
                                        case Keyboard::D:
                                                if (Keyboard::isKeyPressed(Keyboard::D)) {
                                                        const Vector2f spritePos = sprite.getPosition();
                                                        sprite.setPosition(spritePos.x + 5, spritePos.y);
                                                }
                                                break;
                                        }
                                }
                                break;
                }
       
                window.clear(Color(0,0,0,255));

                window.draw(sprite);
                window.display();
        }      
return 0;
 

Flaze07

  • Jr. Member
  • **
  • Posts: 64
    • View Profile
    • Email
Re: two keyboard keys pressed at the same time?
« Reply #1 on: July 23, 2017, 12:39:15 pm »
well...using event for it can be tough but I think it can be done...I'll try to experiment on it

in the mean time
    while (window.isOpen())
    {
            sf::Event eventCheck;
            while (window.pollEvent(eventCheck))
            {
                if (event.type == sf::Event::Closed) window.close();
            }

            if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
            {
                sprite.move(0, -5);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
            {
                sprite.move(-5, 0);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
            {
                sprite.move(0, 5);
            }
            if (sf::Keyboard::isKeyPressed(sf::Keyboard::D))
            {
                sprite.move(5, 0);
            }

            window.clear();
            window.draw(sprite);
            window.display();
    }
return 0;

 
« Last Edit: July 23, 2017, 12:46:45 pm by Flaze07 »

AlexAUT

  • Sr. Member
  • ****
  • Posts: 396
    • View Profile
Re: two keyboard keys pressed at the same time?
« Reply #2 on: July 23, 2017, 09:45:34 pm »
well...using event for it can be tough but I think it can be done...I'll try to experiment on it

You would need to store the state of each key (e.g. std::array<bool, sf::Keyboard::KeyCount>). On the KeyPressed Event you set it to true for the key which fired the event, and in the KeyReleased you will reset the bool to false. This does also decouple your input checking from the input handling.

However events will be a tiny bit delayed because they have to go throught the OS/SFML event queue. So for a game you should use the sf::Input::isKeyPressed function for keys which sate changes rapidly. (or for all keys and change it to a hybrid system if the polling of many keys is a problem)
 


AlexAUT

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: two keyboard keys pressed at the same time?
« Reply #3 on: July 23, 2017, 11:42:18 pm »
Although moving "5" right and "5" up at the same time will move it diagonally, you may want to be more mindful of the actual speed of that movement. If you want a constant speed of direction, test to see if there is movement on both axes; if so, multiply both values by 0.707 (approx.) : sin(45°) or cos(45°).

You might want to also store that "5" as a constant to allow changing that value elsewhere only once. Saying that, it's possible you may want to change it to a variable and allow its speed to be altered.

Here's a short and easy-to-understand example of that stuff in action (the highlighted section):
https://github.com/Hapaxia/SfmlSnippets/blob/230367537d0c56958483221d9927df9802c30ac0/RectangularBoundaryCollision/example.cpp#L136-L148
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything