O'Reilly logo

Game Programming using Qt 5 Beginner's Guide - Second Edition by Lorenz Haas, Witold Wysota, Pavel Strakhov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Input processing

A usual approach in games is to read input events and call functions responsible for actions associated with particular events:

void Scene::keyEvent(QKeyEvent *event) {
    switch(event->key()) {
    case Qt::Key_Right:         player->goRight(); break;
    case Qt::Key_Left:          player->goLeft();  break;
    case Qt::Key_Space:         player->jump();    break;
    // ...
    }
} 

This, however, has its drawbacks, one of which is the need to check events at even periods of time. This might be hard and is certainly not a declarative approach.

We already know that Qt Quick handles keyboard input via the Keys attached property. It is possible to craft QML code similar to the one just presented, but the problem with such an approach is that the faster the player taps keys ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required