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. Strange delay of keyboard input
Forum Updated to NodeBB v4.3 + New Features

Strange delay of keyboard input

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 317 Views 2 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.
  • H Offline
    H Offline
    Hypergeek
    wrote last edited by
    #1

    Hi all!
    I noticed that the app has significant input lag, more than in other apps.
    I created a test example on Qwindow with the OpenGL context. Use the arrow keys to change the size and number of circles to create a load. Pressing Esc displays the time in milliseconds since the program started: the first number is the value from the event timestamp, the second is the time value obtained in the handler. You can see that the second is delayed by one frame.

    There may be a slight discrepancy because the starting time (stored in the constructor) may not be exactly the same as the one used by the event mechanism. But it's clear that the handler is always 1 frame behind the timestamp. This is understandable, but it's just the top of the iceberg. Actual response to a keypress lags by 4 or 5 frames.

    So, for example, you can add a load with arrow keys so that the frame takes, say, 400 ms to draw, and then press Esc—the program responds not after the expected 400 ms, but after about 2 seconds. So, four whole frames pass without a response. Under low loads, this four-frame delay is also present.

    I tried receive keystrokes through winapi (GetAsyncKeyState) instead of events, directly in the rendering function, just before drawing. Basically, it's the same! And the same four-frame delay.

    It seems like the frames themselves are queued up before being displayed and aren't drawn immediately. This is despite the fact that I have a gaming monitor with zero input lag. I also tried enabling low-latency mode in the video card settings—it had no effect.

    S 1 Reply Last reply
    0
    • H Offline
      H Offline
      Hypergeek
      wrote last edited by Hypergeek
      #2

      Zip file with test project: https://drive.google.com/file/d/1RKtHNyAXhCSQpAEb5EYlq_UGMsqLGKLu/view?usp=sharing

      Is any ways to remove this input delay?

      1 Reply Last reply
      0
      • Kent-DorfmanK Offline
        Kent-DorfmanK Offline
        Kent-Dorfman
        wrote last edited by
        #3

        After spending about 20 minutes rewriting your code and .pro file so that it compiles: you are stepping on the keyPressEvent handler with a less strict event() handler. You already have the timer updating the render for you. why are you trapping a generic event to trigger render() too?

        I light my way forward with the fires of all the bridges I've burned behind me.

        H 1 Reply Last reply
        0
        • H Hypergeek

          Hi all!
          I noticed that the app has significant input lag, more than in other apps.
          I created a test example on Qwindow with the OpenGL context. Use the arrow keys to change the size and number of circles to create a load. Pressing Esc displays the time in milliseconds since the program started: the first number is the value from the event timestamp, the second is the time value obtained in the handler. You can see that the second is delayed by one frame.

          There may be a slight discrepancy because the starting time (stored in the constructor) may not be exactly the same as the one used by the event mechanism. But it's clear that the handler is always 1 frame behind the timestamp. This is understandable, but it's just the top of the iceberg. Actual response to a keypress lags by 4 or 5 frames.

          So, for example, you can add a load with arrow keys so that the frame takes, say, 400 ms to draw, and then press Esc—the program responds not after the expected 400 ms, but after about 2 seconds. So, four whole frames pass without a response. Under low loads, this four-frame delay is also present.

          I tried receive keystrokes through winapi (GetAsyncKeyState) instead of events, directly in the rendering function, just before drawing. Basically, it's the same! And the same four-frame delay.

          It seems like the frames themselves are queued up before being displayed and aren't drawn immediately. This is despite the fact that I have a gaming monitor with zero input lag. I also tried enabling low-latency mode in the video card settings—it had no effect.

          S Offline
          S Offline
          SimonSchroeder
          wrote last edited by
          #4

          @Hypergeek said in Strange delay of keyboard input:

          But it's clear that the handler is always 1 frame behind the timestamp. This is understandable

          What's also understandable that usually you'll be using at least double buffering: Showing one buffer and writing to a second one then switching. That'll add one other frame to the delay. There is also triple-buffering, but it is highly unlikely that this is actually used. I don't have an explanation ready to explain further frame delays.

          1 Reply Last reply
          0
          • Kent-DorfmanK Kent-Dorfman

            After spending about 20 minutes rewriting your code and .pro file so that it compiles: you are stepping on the keyPressEvent handler with a less strict event() handler. You already have the timer updating the render for you. why are you trapping a generic event to trigger render() too?

            H Offline
            H Offline
            Hypergeek
            wrote last edited by Hypergeek
            #5

            @Kent-Dorfman said in Strange delay of keyboard input:

            After spending about 20 minutes rewriting your code and .pro file so that it compiles

            Thanks! But why didn't my code compile? It's strange. My Qt version 5.15.2.

            @Kent-Dorfman said in Strange delay of keyboard input:

            you are stepping on the keyPressEvent handler with a less strict event() handler. You already have the timer updating the render for you. why are you trapping a generic event to trigger render() too?

            Yes, this is just old code. It can be removed, and it doesn't really affect rendering.

            @SimonSchroeder said in Strange delay of keyboard input:

            Showing one buffer and writing to a second one then switching. That'll add one other frame to the delay. There is also triple-buffering

            I also tried to change it by setSwapBehavior (tried all 4 variants) - this had no effect on delay.

            Kent-DorfmanK 1 Reply Last reply
            0
            • Kent-DorfmanK Offline
              Kent-DorfmanK Offline
              Kent-Dorfman
              wrote last edited by
              #6

              As a larger issue, you should not use a framework like Qt for metering timing windows with periods less than about 25ms. The OS scheduling jitter inhibits accurate data collection. The std-dev between samples will quickly increase when you use small period event timers or track the system clock at the ms level. If you require ms level accuracy for event windows or metrics then you have to do things like run in realtime schedule mode and not run other processes concurrently. The framework is fine for general user interfaces but not for high framerate game development.

              I light my way forward with the fires of all the bridges I've burned behind me.

              H 1 Reply Last reply
              0
              • H Hypergeek

                @Kent-Dorfman said in Strange delay of keyboard input:

                After spending about 20 minutes rewriting your code and .pro file so that it compiles

                Thanks! But why didn't my code compile? It's strange. My Qt version 5.15.2.

                @Kent-Dorfman said in Strange delay of keyboard input:

                you are stepping on the keyPressEvent handler with a less strict event() handler. You already have the timer updating the render for you. why are you trapping a generic event to trigger render() too?

                Yes, this is just old code. It can be removed, and it doesn't really affect rendering.

                @SimonSchroeder said in Strange delay of keyboard input:

                Showing one buffer and writing to a second one then switching. That'll add one other frame to the delay. There is also triple-buffering

                I also tried to change it by setSwapBehavior (tried all 4 variants) - this had no effect on delay.

                Kent-DorfmanK Offline
                Kent-DorfmanK Offline
                Kent-Dorfman
                wrote last edited by
                #7

                @Hypergeek said in Strange delay of keyboard input:

                @Kent-Dorfman said in Strange delay of keyboard input:

                After spending about 20 minutes rewriting your code and .pro file so that it compiles

                Thanks! But why didn't my code compile? It's strange. My Qt version 5.15.2.

                I built it with qt6 and the pro file was missing the opengl module. You had a malformed connect() statement. Finally, your header includes didn't provide for QOpenGLPaintDevice.

                I light my way forward with the fires of all the bridges I've burned behind me.

                1 Reply Last reply
                0
                • Kent-DorfmanK Kent-Dorfman

                  As a larger issue, you should not use a framework like Qt for metering timing windows with periods less than about 25ms. The OS scheduling jitter inhibits accurate data collection. The std-dev between samples will quickly increase when you use small period event timers or track the system clock at the ms level. If you require ms level accuracy for event windows or metrics then you have to do things like run in realtime schedule mode and not run other processes concurrently. The framework is fine for general user interfaces but not for high framerate game development.

                  H Offline
                  H Offline
                  Hypergeek
                  wrote last edited by Hypergeek
                  #8

                  @Kent-Dorfman said in Strange delay of keyboard input:

                  You had a malformed connect() statement

                  Why malformed? Used form allowed in docs.

                  @Kent-Dorfman said in Strange delay of keyboard input:

                  The framework is fine for general user interfaces but not for high framerate game development

                  Is Qt Quick more suitable for it, or has same delays?

                  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