Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to process several key events at the same time?
Qt 6.11 is out! See what's new in the release blog

How to process several key events at the same time?

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 3 Posters 7.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • raven-worxR raven-worx

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

    S Offline
    S Offline
    Suares
    wrote on last edited by
    #12

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

    raven-worxR 1 Reply Last reply
    0
    • QT-static-prgmQ Offline
      QT-static-prgmQ Offline
      QT-static-prgm
      wrote on last edited by
      #13

      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

      1 Reply Last reply
      0
      • S Suares

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

        raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #14

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

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        S 1 Reply Last reply
        0
        • raven-worxR raven-worx

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

          S Offline
          S Offline
          Suares
          wrote on last edited by
          #15

          @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
          {
          }
          
          raven-worxR 1 Reply Last reply
          0
          • S Suares

            @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
            {
            }
            
            raven-worxR Offline
            raven-worxR Offline
            raven-worx
            Moderators
            wrote on last edited by
            #16

            @Suares
            yes, but still it's not clear how the actual moving is implemented...

            --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
            If you have a question please use the forum so others can benefit from the solution in the future

            1 Reply Last reply
            1
            • QT-static-prgmQ Offline
              QT-static-prgmQ Offline
              QT-static-prgm
              wrote on last edited by
              #17

              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;
              }
              
              1 Reply Last reply
              0
              • S Offline
                S Offline
                Suares
                wrote on last edited by
                #18

                Please wait. I need more time to implement real moving for my tank.
                I'll show you my way as soon as possible.

                1 Reply Last reply
                0
                • QT-static-prgmQ Offline
                  QT-static-prgmQ Offline
                  QT-static-prgm
                  wrote on last edited by
                  #19

                  Man, how you know you stop moving, when you never implemented moving???

                  S 1 Reply Last reply
                  0
                  • QT-static-prgmQ QT-static-prgm

                    Man, how you know you stop moving, when you never implemented moving???

                    S Offline
                    S Offline
                    Suares
                    wrote on last edited by
                    #20

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

                    1 Reply Last reply
                    0
                    • QT-static-prgmQ Offline
                      QT-static-prgmQ Offline
                      QT-static-prgm
                      wrote on last edited by
                      #21

                      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

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved