Solved How to process several key events at the same time?
-
Have you ever heard about Battle City game? In this game you can move (left, up, right and down) your tank and make a shot (fire). I'm working on User Input module and one problem happens when I hold one key, and press another at the same time. If I move my tank and press shot key (space), it stops moving.
I created own KeyEventFilter class where override eventFilter method:bool KeyEventFilter::eventFilter(QObject *, QEvent *event) { bool result = false; if (event->type() == QEvent::KeyPress) { QKeyEvent *ke = static_cast<QKeyEvent *>(event); const auto key = ke->key(); qDebug() << "QEvent::KeyPress"; switch (key) { case Qt::Key_Left: case Qt::Key_Up: case Qt::Key_Right: case Qt::Key_Down: case Qt::Key_Space: if (!ke->isAutoRepeat()) { _keys.insert(key); // _keys is std::set<int> } result = true; break; } } else if (event->type() == QEvent::KeyRelease) { QKeyEvent *ke = static_cast<QKeyEvent *>(event); const auto key = ke->key(); qDebug() << "QEvent::KeyRelease"; switch (key) { case Qt::Key_Left: case Qt::Key_Up: case Qt::Key_Right: case Qt::Key_Down: case Qt::Key_Space: _keys.erase(key); result = true; break; } } if (result) { for (const auto& key : _keys) { emit Send(MakeCommand(key)); // Send() is a signal. MakeCommand() creates command object according to pressed key. } } return result; }
Here are messages from debug (<-- - my comments):
<-- Press Down key.
QEvent::KeyPress
"MoveDownCommand::Execute"
... <- Still holding.
QEvent::KeyPress
"MoveDownCommand::Execute"
<-- Press 2nd key - Space to fire.
QEvent::KeyPress
"ShotCommand::Execute"
"MoveDownCommand::Execute"
QEvent::KeyRelease
"MoveDownCommand::Execute"
<-- Here I still holding Down key but there are no events except one above.Could you please help me how to move tank and fire at the same time?
Or is there maybe another good way to create key event handler? -
I'd always run with x and y by a speedvariable. Pressing left/right key will set the speed variable to -1/1, releasing the key, set it to 0. You can do that through signal/slot
(speedXplus1 - press right and release left, speedXminus1 - press left and release right)
Pressing space send an other signal to fire -
@QT-static-prgm, thank you for answer.
I'll play around with your proposition as soon as possible. -
@Suares
are you sure this isn't a bug in your game logic?
I mean since the key handling looks correct that you simply stop moving your tank as soon as another command comes in? -
As for the first question I'm not sure. I start from User Input module. Now game doesn't have any GUI and update logic. My main loop is very simple:
while (true) { auto cmd = processInput(); // I'm working here. update(cmd); // Has only one call cmd->Execute(); render(); // Empty. delay(); }
As for second one, I don't want to stop moving my tank as soon as another command comes in.
- Press only one of the move keys (left, up, right, down) - everything is okay.
- Press only fire (space) - everything is okay too.
- Hold move key and press (not hold) fire key - failure. I can see fire and move commands in debug messages, but after it my tank can't continue moving while I still hold move button. And I noticed there are no more key events in debug messages after pressing and releasing space key for fire. Hmmm...
-
@Suares said in How to process several key events at the same time?:
As for second one, I don't want to stop moving my tank as soon as another command comes in.
yes i know.
As i said, i simply suspect a bug in your game logic in the key event processing ->processInput()
-
Okay, I'll check it once again.
-
best way for help is to upload your code somewhere (github) and give us a link to the source. Additionally you can point to the (in your opinion) important part of the code. That way we can easier look for other function, you may thought are not important
-
It'll be very nice. When I come home I'll push project to github and notify you.
-
Hello! I've uploaded project to github.
Game.cpp line 8 - create and install my custom event filter.
Game.cpp line 22 - main game loop.
KeyEventFilter.cpp line 14 - overridden eventFilter method.
And UserInput class contains queue with user commands.Thank you for help!
-
@Suares
your Command::Execute() methods are empty?
I guess your Commands (shared pointers) run out of scope as soon as you execute the next command. -
Not exactly. They have one line:
qDebug() << ...
As for second your sentence. I'm sorry I didn't quite get that. Could you please explain?
-
I wonder about the executes, too. You just print things on qdebug, so how you know you are no longer moving when firing?? You're actually never start moving
-
@Suares said in How to process several key events at the same time?:
As for second your sentence. I'm sorry I didn't quite get that. Could you please explain?
But it would be interesting how you exactly implemented the moving. Because thats essential to know when it stops doing what it's supposed to be doing ;)
I mean that the commands maybe get deleted because no object holds a reference to it anymore. But again this needs insight how the commands actually work.
-
If user stop pressing keys - NoCommand will be executed.
Please look at UserInput.cpp line 27:
return (cmd) ? cmd : std::make_shared<NoCommand>();
If there is no command from user - queue is empty that is why I return NoCommand.
Definition of this command is:
void NoCommand::Execute() noexcept { }
-
@Suares
yes, but still it's not clear how the actual moving is implemented... -
you need to have something like this:
void updateScene() { littleMan.positionX += moveX; // you remember what i told you about the moveX variable and the signal? littleMan.positionY += moveY; for(auto& it : boldList) it.update(); // while this update function will move your bold one step forward in the fire direction. }
and your slot for firing will add an bold to the boldList. An bold can be a struct like this:
struct Bold { QPoint position; QVector2D direction; int speed; }
-
Please wait. I need more time to implement real moving for my tank.
I'll show you my way as soon as possible. -
Man, how you know you stop moving, when you never implemented moving???
-
I implemented movement. If don't belieave you can build project :)
Tank moves, but I can't move and fire at the same time. I want to fix it.