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.


  • Moderators

    @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?



  • @raven-worx

    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.

    1. Press only one of the move keys (left, up, right, down) - everything is okay.
    2. Press only fire (space) - everything is okay too.
    3. 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...

  • Moderators

    @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()



  • @raven-worx

    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



  • @QT-static-prgm

    It'll be very nice. When I come home I'll push project to github and notify you.



  • @QT-static-prgm

    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!


  • Moderators

    @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.



  • @raven-worx

    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


  • Moderators

    @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.



  • @raven-worx

    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
    {
    }
    

  • Moderators

    @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???



  • @QT-static-prgm

    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.



  • D:\workspaces\Qt_default\BattleCity-master\include\Game.h:44: Fehler: 'atomic' in namespace 'std' does not name a template type

    Is it just me, or is the code a little unreadable? @raven-worx

    @Suares why are you always using {} instead of ()??

    KeyEventFilter* kef = new KeyEventFilter{ _userTank, this };
    Game{ argc, argv }
    

    ok this code is really ugly (in my opinion) but if i understand it right you are doing this:

    get key press and generate a Commando from it,
    save them in a queue,
    remove the commando from the queue and execute the command, till the list is empty.

    So if you press the key it is on the queue once. you take it, execute and forgot about it.

    Again why don't you make something like this:

    class tank{
    ...
    QPoint m_position;
    QVector2D m_speed = {0,0};
    
    void addSpeed(QVector2D speed){
    m_speed += speed;
    }
    update()
    {
    m_position += m_speed;
    }
    
    }
    
    struct Bold{
    QPoint(position);
    QVecotr2D(direction);
    int speed;
    }
    
    MainWindow(){
    
    private:
    
    tank* myTank = new tank;
    QVector<Bold> m_bolds;
    
    addbold(tank* tank)
    {
    Bold newBold = {tank.getPos, tank.getDirection, 100};
    m_bolds.pushback(newBold);
    }
    
    mainLoop()
    {
    myTank->update();
    
    for(auto& it : m_bolds)
      it.position += it.direction * speed;
    
    render();
    }
    
    keyPressEvent()
    {
     if(upPress)
      tank->addSpeed(0,1);
    else if(upRelease)
      tank->addSpeed(0,-1);
    if(downPress)
      tank->addSpeed(0,-1);
    else if(downRelease)
      tank->addSpeed(0,1);
    else  if(leftPress)
      tank->addSpeed(-1,0);
    else if(leftRelease)
      tank->addSpeed(1,0);
    if(rightPress)
      tank->addSpeed(1,0);
    else if(rightRelease)
      tank->addSpeed(-1,0);
    else if(spacePress)
      addBold(tank);
    
    }
    
    }
    
    }
    
    }
    

    you need just your tank class, your MainWindow and a struct for the bold. Your Project has: 7(!!) Classes


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.