1

Let me first show you a piece of code:

void PaddleItem::keyPressEvent(QKeyEvent *e)
{
qDebug() << timer.elapsed()-lastTime;
lastTime = timer.elapsed();
if(velocityX < maxAbsoulteVelocity && velocityX > -maxAbsoulteVelocity)
{
    if(e->key() == Qt::Key_Left)
    {
        velocityX -= 15;
        qDebug() << "LEFT " << velocityX;
    }
    if(e->key() == Qt::Key_Right)
    {
        velocityX += 15;
        qDebug() << "RIGHT " << velocityX;
    }
}

I measured the interval between the first and the second occurance of QKeyEvent after pressing the key - I found it is 500 ms. Further intervals are only about 33 ms. So, once again to be clear - I am pressing left arrow and it goes like that: Event - 500 ms - Event - ~33 ms - Event - ~33 ms - Event etc. It is quite problematic for me since I want my paddle to move smoothly - and this lag makes it impossible. How can I deal with that?

pklimczu
  • 626
  • 1
  • 6
  • 15
  • Looks this is the intended behaviour of key events, to avoid accidental multiple presses (which would happen *always* if you had the 33ms initial delay). Might be configurable – hauron Jul 14 '16 at 09:18

1 Answers1

1

That's how autorepeat feature of your keyboard, and every other standard PC keyboard in the world, works. You should never use use it or rely on it. It's for user's convenience, not programmer's. A user can change or disable it at any time.

Instead, detect only the first key press in the series and ignore all others. The series lasts until a key release event. Use your own timer to advance your position at a certain rate.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243