Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Game Development
  4. Big Issue with Qt key inputs for gaming
QtWS25 Last Chance

Big Issue with Qt key inputs for gaming

Scheduled Pinned Locked Moved Game Development
9 Posts 5 Posters 13.0k 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.
  • D Offline
    D Offline
    Dynamite101
    wrote on last edited by
    #1

    I have been learning Programming for 12 months and while learning Qt decided this is the library i also want to use for game dev. I use PyQt so im not sure if this problem is present with Qt on C++. Firstly i made a small demo with a sprite that responds to key inputs. I sub classed QWidget and reimplimented its PaintEvent. The issue is this: my sprite can move left or right based on a key press, but when, say a key is pressed and held to keep moving left, it moves left once, stops for a split second, then keeps moving left. This pause in between the press has a horrible, non fluid effect for gaming and controls. I then did a quick space invaders clone and its hell to dodge the enemy fire.

    I decided to quickly try the same demo using the QGraphicsView classes. Sadly the same issue is present with the key inputs. When held, there is still that delay before it starts responding. So quick shifts in movement is impossible. Before i started learning Qt i tried this same demo with another library and the key inputs were spot on.

    Im wondering if anything can be done about this? Other than say static board games, it will be impossible to make fast dynamic games using Qt it seems. Im wondering if this is just a PyQt issue, or is it also present in C++? could someone please check? Can anyone please tell me If this is a bug that can be fixed, or are we stuck with it?

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

      It has nothing to do with Qt or C++. It's how keyboards work. Imagine how annoying it would be to type if it wasn't there - you would basically wwwriiittee muulltiiippllle llleettttterrrssss if you're not fast enough :)

      That being said games require a little different treatment and some libraries that target game dev do that for you under the covers.
      With games you usually do something like key array that holds a boolean state whether they're pressed or not. You set a value to true on keypress and to false on key release event. Then with a help of a timer you poll for the state in whatever intervals you want, be it regular or not.
      It's also useful to create such array because you can redirect multiple controllers to the same values, eg. a keyboard and a game pad or mouse.

      1 Reply Last reply
      0
      • D Offline
        D Offline
        Dynamite101
        wrote on last edited by
        #3

        Thanks so much for the detailed response. I had no idea this was how it worked under the covers.
        The solution sounds a little out of my league but i can give it a try. The timer part i sort of understand but that's where i'm a bit unsure. Thanks again

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

          Sorry if I made it sound scarry. It's actually as easy as this:

          class MyWidget : public QWidget
          {
              Q_OBJECT
          public:
              MyWidget()
              { setFocusPolicy(Qt::StrongFocus); startTimer(1000/60); }
          
              void keyPressEvent(QKeyEvent *e)
              { keys[e->key()] = true; QWidget::keyPressEvent(e); }
          
              void keyReleaseEvent(QKeyEvent *e)
              { keys[e->key()] = false; QWidget::keyReleaseEvent(e); }
          
              void timerEvent(QTimerEvent *)
              { if(keys[Qt::Key_Up]) /* some game logic */; }
          
          private:
              QMap<int, bool> keys;
          };
          

          As you can see input can be put into the map from wherever, key events, mouse events, gamepad or whatever.
          Of course there are considerations, like map performance or accessing the map in multi-threaded environment but that's the basic idea.

          EDIT: I don't know Python that well but you should be able to translate it easily

          F 1 Reply Last reply
          5
          • D Offline
            D Offline
            Dynamite101
            wrote on last edited by
            #5

            Oh great thanks for the code example! I'm going to give it a go. I've never come across QMap in Python/PyQt, i wonder if it exists but it seems like a normal Python dictionary so i'm sure i can compromise if it's missing. Thanks for all your help and time.

            1 Reply Last reply
            0
            • D Offline
              D Offline
              Dynamite101
              wrote on last edited by
              #6

              [quote author="Chris Kawa" date="1375116449"]Sorry if I made it sound scarry. It's actually as easy as this:

              [/quote]

              You're the best. I've got it working just like you said and i can't believe it! I thought i would have to learn an actual game library but i'm comfortable with Qt and even though i'm new to programming as a whole, I'm quite confident that Qt is capable of putting together a decent game.

              Thanks!

              1 Reply Last reply
              0
              • J Offline
                J Offline
                jiangcaiyang
                wrote on last edited by
                #7

                Here I've made two blogs concerning this issue:
                http://blog.csdn.net/gamesdev/article/details/8724172
                http://blog.csdn.net/gamesdev/article/details/8738746

                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  Sorry if I made it sound scarry. It's actually as easy as this:

                  class MyWidget : public QWidget
                  {
                      Q_OBJECT
                  public:
                      MyWidget()
                      { setFocusPolicy(Qt::StrongFocus); startTimer(1000/60); }
                  
                      void keyPressEvent(QKeyEvent *e)
                      { keys[e->key()] = true; QWidget::keyPressEvent(e); }
                  
                      void keyReleaseEvent(QKeyEvent *e)
                      { keys[e->key()] = false; QWidget::keyReleaseEvent(e); }
                  
                      void timerEvent(QTimerEvent *)
                      { if(keys[Qt::Key_Up]) /* some game logic */; }
                  
                  private:
                      QMap<int, bool> keys;
                  };
                  

                  As you can see input can be put into the map from wherever, key events, mouse events, gamepad or whatever.
                  Of course there are considerations, like map performance or accessing the map in multi-threaded environment but that's the basic idea.

                  EDIT: I don't know Python that well but you should be able to translate it easily

                  F Offline
                  F Offline
                  freddy91
                  wrote on last edited by
                  #8

                  @Chris-Kawa On Qt5.5 this is still very effective! Thanks

                  1 Reply Last reply
                  0
                  • A Offline
                    A Offline
                    amzounslideslide
                    Banned
                    wrote on last edited by amzounslideslide
                    #9
                    This post is deleted!
                    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