-
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.
-
I guess you could check if one of the two keys is pressed while the other hasn't been released yet
-
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.
-
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
-
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 (http://www.bromeon.ch/libraries/thor/v2.0/tutorial-actions.html) 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"];
-
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:
$ ./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.
-
@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.