Results 1 to 4 of 4

Thread: Smooth continuous keyboard input and capturing multiple keys simultaneously

  1. #1
    Join Date
    Nov 2012
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Smooth continuous keyboard input and capturing multiple keys simultaneously

    I'm using the keyboard to navigate in an OpenGL scene. The problem is, when I hold down the "w" key to move forward, I edge forward a tiny bit first, pause, then glide forward smoothly. That's just how keyboard input works in Windows - when you hold down a key, there's a pause before it begins repeating. It's useful for, say, typing, but not much good for 3D navigation. Professional games override this somehow. I was wondering if there was a way to override this behavior with Qt, to eliminate this brief pause before the key starts repeating.

    Also, in games you can for instance press "w" and "d" simultaneously to move diagonally forward and to the right. In Qt the way I have it set up now (just monitoring for key down events) I can only press one key at a time. So if I mashed "w" and "d" at the same time, one would trigger before the other, and I would move either forward or right, but not both. Is there some way I could work around that and detect when multiple keys are held down?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Smooth continuous keyboard input and capturing multiple keys simultaneously

    Intercept both keyPress and keyRelease events. In the keyPressEvent set a flag that a particular key is down, in keyReleaseEvent lower that flag. Then in your game loop (if you don't have one, just use a timer with a small period) simply check if the flag is raised or not and navigate your scene. Of course you will have to disable key repeating.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    fiodis (11th December 2012)

  4. #3
    Join Date
    Nov 2012
    Posts
    18
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Smooth continuous keyboard input and capturing multiple keys simultaneously

    Hmm, I never thought of it that way. It works perfectly, thanks! I guess I was hung up on the idea of somehow overriding the way Qt recieved keyboard events.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Smooth continuous keyboard input and capturing multiple keys simultaneously

    Quote Originally Posted by fiodis View Post
    Hmm, I never thought of it that way. It works perfectly, thanks! I guess I was hung up on the idea of somehow overriding the way Qt recieved keyboard events.
    Never rely in games on events being delivered in constant time spans or in moments when you expect them to be delivered (that especially counts for repainting). Always run a timer controlled loop. Only then you'll be certain how fast your game really "plays". And it's not enough to have a simple timer that triggers 10 times per second to be sure you get 10 game ticks per second. You'll get at most 10 ticks per second. What you should do is you should run a single shot timer and note the current time. When the timer fires, check the current time and calculate the difference from the last timeout. It might be that more than one tick has passed and also your next timeout may need to be adjusted because last tick might have used more time than you think.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. Replies: 1
    Last Post: 3rd January 2012, 22:15
  2. How can I set the keyboard input mode?
    By Santiago in forum Newbie
    Replies: 3
    Last Post: 15th November 2011, 14:21
  3. Painting in multiple QWidgets simultaneously
    By edepetete in forum Qt Programming
    Replies: 0
    Last Post: 17th October 2011, 18:21
  4. Continuous read of text from an input file
    By ttvo in forum Qt Programming
    Replies: 1
    Last Post: 2nd June 2009, 01:09
  5. Keyboard Capturing
    By ToddAtWSU in forum Qt Programming
    Replies: 2
    Last Post: 29th June 2007, 07:37

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.