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 make mouseMoveEvent and keyPressEvent to be called interchangeably?
QtWS25 Last Chance

How to make mouseMoveEvent and keyPressEvent to be called interchangeably?

Scheduled Pinned Locked Moved Solved General and Desktop
qeventmouseeventkeypresseventqglwidget
8 Posts 3 Posters 3.1k Views
  • 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.
  • B Offline
    B Offline
    BoboFuego
    wrote on last edited by
    #1

    I have a program that reacts to mouse movements and keypress events. My problem is that when a key is already pressed, most of the mouse movements are being ignored, or maybe one mouse-move event is handled every about ~20 handlings of keypress.
    How to make the both events to be equally likely handled when they both are active?

    My Qt version is 5.11.1 and I'm using it on Debian 9 (stretch). Any help please?

    1 Reply Last reply
    0
    • Chris KawaC Offline
      Chris KawaC Offline
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      That doesn't seem like something that could be configured. Mouse and keyboard are separate devices and Qt handles them based on the events sent from the OS. Those events are kinda independent of each other,, so if you want to get them at some fixed rate in Qt you would have to make your OS send those events that way, which, again, sounds unlikely to be configurable (though I'm not a Linux guy, so who knows).

      Maybe there's some other approach we can find. What do you need that for?

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BoboFuego
        wrote on last edited by
        #3

        I do have a 3d scene with Opengl, and I'm using the keyboard to move forward and backward, and the mouse pointer to change the direction. The problem is that in order to change the direction while moving, I have to release the pressed key on the keyboard, as in the most of cases the keyPressEvents somehow get over the mouseMoveEvents.
        Here is how my draft code looks like:

        MyWidget.h

        class MyWidget : public QGLWidget, protected QOpenGLFunctions_3_0
        {
            Q_OBJECT
        public:
            // some other stuff
        
        protected:
            virtual void mouseMoveEvent(QMouseEvent*) override;
            virtual void keyPressEvent(QKeyEvent*) override;
        
           // some other stuff
        
        private:
        
           // some other stuff
        
        

        MyWidget.cpp

        void MyWidget::keyPressEvent(QKeyEvent* ev)
        {
            // move forward, backward
        }
        
        void MyWidget::mouseMoveEvent(QMouseEvent *ev)
        {
           // change direction
        }
        

        I'm wondering if it is even possible to call mouseMoveEvent inside the keyPressEvent , so to make sure that after every keypress handling the mouse event will be handled as well?

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by Chris Kawa
          #4

          Mouse events and keyboard events have nothing to do with each other and you shouldn't try to tie them in any way.

          That being said you should not tie movement directly to key press events either because of how keyboards work - there's an initial key press, and if you hold the key, after a short break you get a stream of additional key presses at a much higher rate. This is because keyboards were originally designed for typing, not camera manipulation in 3D environments.

          For camera control you should handle input a little differently. Mouse moves you can handle directly in the move events, but keyboard needs special treatment. I already described that in another thread a couple years back so have a look: Big Issue with Qt key inputs for gaming. In short you set some variable to true on press and to false on release and then poll that variable at a constant rate (e.g. with a timer). Don't tie directly to key presses or releases, as keyboard rates are hardware and software dependent and you don't want to have your camera move slower or faster on different computers.

          1 Reply Last reply
          2
          • Chris KawaC Offline
            Chris KawaC Offline
            Chris Kawa
            Lifetime Qt Champion
            wrote on last edited by Chris Kawa
            #5

            Oh, and another thing that may be related to your problem - don't update your 3D view in these input handlers. Both mouse and keyboard events come at a much higher rate than the refresh rate of a standard display. If you're trying to "redraw" on every key press you're probably choking the machine and it can't handle mouse events at a reasonable rate anymore.

            If you haven't already, decouple your drawing from input. As I mentioned before - store keyboard state in some variables and accumulate the change in direction from mouse events in some variable and then actually process that input at some fixed interval which is lower or equal to that of the refresh rate of your target display (usually 60Hz) e.g. with a timer.

            B 1 Reply Last reply
            2
            • B Offline
              B Offline
              BoboFuego
              wrote on last edited by
              #6

              Yes i already do the movement/rotation at a constant rate. But I think the idea of using press/release key events to set some flags was just on the point. I think it should solve my problem, will try it later anyway.
              Thanks for the info :-)

              1 Reply Last reply
              0
              • Chris KawaC Chris Kawa

                Oh, and another thing that may be related to your problem - don't update your 3D view in these input handlers. Both mouse and keyboard events come at a much higher rate than the refresh rate of a standard display. If you're trying to "redraw" on every key press you're probably choking the machine and it can't handle mouse events at a reasonable rate anymore.

                If you haven't already, decouple your drawing from input. As I mentioned before - store keyboard state in some variables and accumulate the change in direction from mouse events in some variable and then actually process that input at some fixed interval which is lower or equal to that of the refresh rate of your target display (usually 60Hz) e.g. with a timer.

                B Offline
                B Offline
                BoboFuego
                wrote on last edited by
                #7

                @Chris-Kawa Oky, here I recall, those suggestions actually solved my problem, so thanks for the information! The problem was that I was calling repaint() inside mouseMoveEvent and keyPressEvent removing it from there solved the issue, and the program now runs smoothly as expected.

                I don't know how to mark this thread as solved, I didn't see any obvious tool for such a thing from my side, so you may manage that from your side.

                jsulmJ 1 Reply Last reply
                1
                • B BoboFuego

                  @Chris-Kawa Oky, here I recall, those suggestions actually solved my problem, so thanks for the information! The problem was that I was calling repaint() inside mouseMoveEvent and keyPressEvent removing it from there solved the issue, and the program now runs smoothly as expected.

                  I don't know how to mark this thread as solved, I didn't see any obvious tool for such a thing from my side, so you may manage that from your side.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by jsulm
                  #8

                  @BoboFuego said in How to make mouseMoveEvent and keyPressEvent to be called interchangeably?:

                  I don't know how to mark this thread as solved

                  "Topic Tools" at the bottom right side and then "Mark as Solved".

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  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