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

Author Topic: Detect multiple keypress  (Read 10596 times)

0 Members and 1 Guest are viewing this topic.

vankraster

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Detect multiple keypress
« on: June 09, 2014, 02:03:32 pm »
Hi,
I want to detect multiple keypress like downArrow and RightArrow I have the code:
 if (event.type == sf::Event::KeyPressed)
                        {
                                if (event.key.code == sf::Keyboard::Key::Escape)
                                        window.close();

                                if (event.key.code == sf::Keyboard::Key::Right)
                                        me.MoveXAxis(1);
                                else if (event.key.code == sf::Keyboard::Key::Left)
                                        me.MoveXAxis(-1);

                                if (event.key.code == sf::Keyboard::Key::Up)
                                        me.MoveYAxis(-1);
                                else if (event.key.code == sf::Keyboard::Key::Down)
                                        me.MoveYAxis(1);
                        }
 

As you can see up-down and left-right are in different IF blocks but if I press Down-Right it only goes on DOWN how can I intercept the right key press ? so the plane can move on diagonal...
Thanks.

Strelok

  • Full Member
  • ***
  • Posts: 139
    • View Profile
    • GitHub
Access violation when referencing a SF::RenderWindow
« Reply #1 on: June 09, 2014, 03:25:24 pm »
I guess you could check if one of the two keys is pressed while the other hasn't been released yet
« Last Edit: June 09, 2014, 03:26:56 pm by Strelok »

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Detect multiple keypress
« Reply #2 on: June 09, 2014, 05:11:19 pm »
Use a couple of bool variables to hold state information about keys.
So, when you get the event that down is pressed, set "down_pressed = true;" and when it is released set it to false.
Do the same for the other keys. Then to check if you are going down and right, check "if (down_pressed && right_pressed) { down_right_do_stuff(); }" etc.

vankraster

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Detect multiple keypress
« Reply #3 on: June 09, 2014, 06:06:56 pm »
Use a couple of bool variables to hold state information about keys.
So, when you get the event that down is pressed, set "down_pressed = true;" and when it is released set it to false.
Do the same for the other keys. Then to check if you are going down and right, check "if (down_pressed && right_pressed) { down_right_do_stuff(); }" etc.

I can't do that because in if block (event.type == sf::Event::KeyPressed) it never catch the right key press, it only catch the down key press when i hold both right and down keys, so a bool is useless here.
I hope I understand what you've suggested to me.
P.S. Sorry for my poor english

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Detect multiple keypress
« Reply #4 on: June 09, 2014, 06:27:50 pm »
In general, I don't think you want events to move a plane, but rather real-time input with sf::Mouse. The code is then structured like:
sf::Vector2f velocity;
if (left key pressed)
   velocity.x -= ...;
if (right key pressed)
   velocity.x += ...;
... // same for up and down, with y

if (both x and y not zero)
   then we move diagonally

Maybe Thor could also help:
thor::ActionMap<std::string> map;
map["left"] = thor::Action(sf::Keyboard::Left);
map["down"] = thor::Action(sf::Keyboard::Down);
map["left+down"] = map["left"] && map["down"];
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Detect multiple keypress
« Reply #5 on: June 09, 2014, 09:49:10 pm »
This is what I meant:

#include <SFML/Window.hpp>
#include <cstdlib>
#include <iostream>

int main()
{
    sf::Window window(sf::VideoMode(800, 600), "My window");

    bool moving_down = false;
    bool moving_right = false;

    enum class MsgType { DOWN, RIGHT, DOWNRIGHT, NOTMOVING, NONE };
    auto last_msg = MsgType::NONE;

    while (window.isOpen()) {
        sf::Event event;
        while (window.pollEvent(event)) {
            switch (event.type) {
            case sf::Event::KeyPressed:
                if (event.key.code == sf::Keyboard::Right)
                    moving_right = true;
                else if (event.key.code == sf::Keyboard::Down)
                    moving_down = true;
                break;
            case sf::Event::KeyReleased:
                if (event.key.code == sf::Keyboard::Right)
                    moving_right = false;
                else if (event.key.code == sf::Keyboard::Down)
                    moving_down = false;
                break;
            case sf::Event::Closed:
                window.close();
                break;
            default:
                break;
            }
        }

        if (moving_down && moving_right) {
            if (last_msg != MsgType::DOWNRIGHT) {
                std::cout << "Moving down & right." << std::endl;
                last_msg = MsgType::DOWNRIGHT;
            }
        } else if (moving_down) {
            if (last_msg != MsgType::DOWN) {
                std::cout << "Moving down." << std::endl;
                last_msg = MsgType::DOWN;
            }
        } else if (moving_right) {
            if (last_msg != MsgType::RIGHT) {
                std::cout << "Moving right." << std::endl;
                last_msg = MsgType::RIGHT;
            }
        } else {
            if (last_msg != MsgType::NOTMOVING) {
                std::cout << "Not moving." << std::endl;
                last_msg = MsgType::NOTMOVING;
            }
        }
    }

    return EXIT_SUCCESS;
}
 

A quick test run seems to give the desired result:

Code: [Select]
$ ./a.out
Not moving.
Moving down.
Not moving.
Moving down.
Moving down & right.
Moving right.
Not moving.
Moving down.
Moving down & right.
Moving down.
Not moving.
Moving right.
Moving down & right.
Moving right.
Not moving.
« Last Edit: June 09, 2014, 11:00:30 pm by Jesper Juhl »

vankraster

  • Newbie
  • *
  • Posts: 5
    • View Profile
    • Email
Re: Detect multiple keypress
« Reply #6 on: June 10, 2014, 11:19:26 am »
@Jesper Juhl
Thanks for help it's what I need. I want to choose your answer as Solved answer and I don't know how..
Thanks.