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. keypressed and keyreleased
Forum Updated to NodeBB v4.3 + New Features

keypressed and keyreleased

Scheduled Pinned Locked Moved Unsolved General and Desktop
25 Posts 6 Posters 3.5k Views 3 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.
  • G Offline
    G Offline
    genli
    wrote on last edited by genli
    #15

    I changed the code but it's the same error :
    void Robot::keyPressEvent(QKeyEvent*keyevent)
    {
    if (keyevent->key() == Qt::Key_Z)
    {

       ui->forward_btn->pressed();
    

    }
    }
    void Robot::keyReleaseEvent(QKeyEvent*keyevent)
    {

     if (keyevent->key() == Qt::Key_Z)
     {
         ui->forward_btn->released();
       }
    

    }
    any idea please ?

    CP71C 1 Reply Last reply
    0
    • G genli

      I changed the code but it's the same error :
      void Robot::keyPressEvent(QKeyEvent*keyevent)
      {
      if (keyevent->key() == Qt::Key_Z)
      {

         ui->forward_btn->pressed();
      

      }
      }
      void Robot::keyReleaseEvent(QKeyEvent*keyevent)
      {

       if (keyevent->key() == Qt::Key_Z)
       {
           ui->forward_btn->released();
         }
      

      }
      any idea please ?

      CP71C Offline
      CP71C Offline
      CP71
      wrote on last edited by
      #16

      @genli
      It seems you have 2 ways to moving the robot, via keyboard and via UI (pushbutton), haven't you?

      G 1 Reply Last reply
      0
      • CP71C CP71

        @genli
        It seems you have 2 ways to moving the robot, via keyboard and via UI (pushbutton), haven't you?

        G Offline
        G Offline
        genli
        wrote on last edited by
        #17

        @CP71 yes that's it

        CP71C 2 Replies Last reply
        0
        • G genli

          @CP71 yes that's it

          CP71C Offline
          CP71C Offline
          CP71
          wrote on last edited by CP71
          #18

          @genli
          The idea is having one place where manage the moving.
          So my idea is the same:

          • having a timer always actived
          • defining a enum or defines for direction
          • in key event or pushbutton event store last moving selection.

          Only for example:

          enum Moving
          {
          enStop,
          enForward,
          enBack,
          enLeft,
          enRight
          }

          Moving moving;

          void MyObject::core()
          {
          ...
          //Moving manager

          switch( moving)
          {
          case enForward:
          move1();
          break;

          case enBack: 
          	move2();
          	break;
          ...
          
               default:
                    stopMoving();
               break;
          

          }
          ...
          }

          void MyObject:: keyReleaseEvent(QKeyEventkeyevent)
          {
          keyPressed = enStop;
          }
          void MyObject::keyPressEvent(QKeyEvent
          keyevent)
          {
          if ( keyEvent->key() == "S" )
          keyPressed = enForward;
          else if ( keyEvent->key() == "D" )
          keyPressed = enBack;
          ...
          }

          //for all 4 buttons write a code like this
          void MyObject::on_forward_btn_pressed()
          {
          keyPressed = enForward;
          ]

          void MyObject::on_forward_btn_relesed()
          {
          keyPressed = enStop;
          ]

          The above code could have error, I write in editor and not in QTCreator.

          For security the stopOperation I insert always in default.

          1 Reply Last reply
          0
          • G genli

            @CP71 yes that's it

            CP71C Offline
            CP71C Offline
            CP71
            wrote on last edited by CP71
            #19

            @genli
            Obviously, you must correctly manage two commands that are opposite ( for example left and right simultaneously ).
            but switch'd already help you for this case ;)

            G 1 Reply Last reply
            0
            • CP71C CP71

              @genli
              Obviously, you must correctly manage two commands that are opposite ( for example left and right simultaneously ).
              but switch'd already help you for this case ;)

              G Offline
              G Offline
              genli
              wrote on last edited by genli
              #20

              @CP71 the thing I can't understand is when I press Z:
              The debug console shows me:
              Forward
              keyReleased
              Forward
              keyReleased
              Forward
              keyReleased
              and when I keep pressing z and I press another button for example R or P the problem solved
              The debug console shows me:
              Forward
              Forward
              Forward
              how i can press a button on the keyboard using code ?

              artwawA CP71C 2 Replies Last reply
              0
              • G genli

                @CP71 the thing I can't understand is when I press Z:
                The debug console shows me:
                Forward
                keyReleased
                Forward
                keyReleased
                Forward
                keyReleased
                and when I keep pressing z and I press another button for example R or P the problem solved
                The debug console shows me:
                Forward
                Forward
                Forward
                how i can press a button on the keyboard using code ?

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by artwaw
                #21

                @genli create QKeyEvent and post it to the event queue https://doc.qt.io/qt-5/qcoreapplication.html#postEvent

                I think the above is overly complicated though. I'd go with a set of flags, either standalone or the vector of bool, and custom signal - let's say keyboardStateChanged() or something.
                In keypressed/keyreleased events you just set the flag and emit the signal.
                The signal it self connected as a queued connection, so it's just processed after the control returns from the keyboard event processing, connected to the slot which in turn, sends movement commands to the remote.
                Less methods, less code the way I see it, clearer approach.

                EDIT: also, try do debug key events to see possible modifiers from the event. Those are platform dependent.

                For more information please re-read.

                Kind Regards,
                Artur

                1 Reply Last reply
                1
                • G genli

                  @CP71 the thing I can't understand is when I press Z:
                  The debug console shows me:
                  Forward
                  keyReleased
                  Forward
                  keyReleased
                  Forward
                  keyReleased
                  and when I keep pressing z and I press another button for example R or P the problem solved
                  The debug console shows me:
                  Forward
                  Forward
                  Forward
                  how i can press a button on the keyboard using code ?

                  CP71C Offline
                  CP71C Offline
                  CP71
                  wrote on last edited by CP71
                  #22

                  @genli
                  I don't know why, if you are pushing Z without release the button you should not read keyReleased.
                  But remember void MyObject:: keyReleaseEvent(QKeyEventkeyevent) is called for any key that is released

                  G 1 Reply Last reply
                  0
                  • CP71C CP71

                    @genli
                    I don't know why, if you are pushing Z without release the button you should not read keyReleased.
                    But remember void MyObject:: keyReleaseEvent(QKeyEventkeyevent) is called for any key that is released

                    G Offline
                    G Offline
                    genli
                    wrote on last edited by
                    #23

                    @CP71 there's a way to do it with signal and slot ?

                    artwawA CP71C 2 Replies Last reply
                    0
                    • G genli

                      @CP71 there's a way to do it with signal and slot ?

                      artwawA Offline
                      artwawA Offline
                      artwaw
                      wrote on last edited by
                      #24

                      @genli I think more flexible approach for you would be to go with the event filter https://doc.qt.io/qt-5/eventsandfilters.html#event-filters

                      For more information please re-read.

                      Kind Regards,
                      Artur

                      1 Reply Last reply
                      1
                      • G genli

                        @CP71 there's a way to do it with signal and slot ?

                        CP71C Offline
                        CP71C Offline
                        CP71
                        wrote on last edited by CP71
                        #25

                        @genli
                        You have several ways to do this, depend on own style, as it often happens there isn't only one way to do something.

                        There's a way to do it with signal and slot ?
                        Yes of course!
                        As @artwaw says you can use event filter.
                        I wouldn't use eventFiler, but not because is not correct, but only because it is not my style, doesn't mean that my style is the best way!

                        The @artwaw's tip is good, in short, think simple, less code you have less bug you will have.

                        My tip is the same, having one point where you manage the moving. How you send the variable, flags, and so on depends only on you and how the project is designed.

                        Normally in my projects, I like to have an application core manager that is independent of UI. The UI and the core communicate between them via signals.

                        But before going ahead, for me, you must understand because you have:
                        Forward
                        keyReleased
                        when wouldn't be so.

                        1 Reply Last reply
                        1

                        • Login

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