2

I have a program where for example if I hold down W then D everything works fine, however if I then release D it seems KeyPressEvent or KeyReleaseEvent do not trigger for w, I have checked this using qDebug, this occurs for any sequence of keys as long as they are pressed and depressed in the order specified above. But it seems that Renderarea is still being called somehow...

void RCcar::keyPressEvent(QKeyEvent* event) {
    //sendMove->sendData(5);
    if (event->key() == Qt::Key_A) {
        Renderarea->is_pressed[0] = true;
        if (Renderarea->is_pressed[1]) {
            sendMove->sendData(4);
        }
        else if (Renderarea->is_pressed[3]) {
            sendMove->sendData(3);
            sendMove->sendData(0);
        }
        else {
            sendMove->sendData(0);
        }
        Renderarea->update();
    }
    if (event->key() == Qt::Key_D) {
        Renderarea->is_pressed[1] = true;
        if (Renderarea->is_pressed[0]) {
            sendMove->sendData(4);
        }
        else if (Renderarea->is_pressed[3]) {
            sendMove->sendData(3);
            sendMove->sendData(1);
        }
        else {
            sendMove->sendData(1);
        }
        Renderarea->update();
    }
    if (event->key() == Qt::Key_S) {
        Renderarea->is_pressed[2] = true;
        if (Renderarea->is_pressed[3]) {
            sendMove->sendData(4);
        }
        else {
            sendMove->sendData(2);
        }

        Renderarea->update();
    }
    if (event->key() == Qt::Key_W) {
        Renderarea->is_pressed[3] = true;
        if (Renderarea->is_pressed[2]) {
            sendMove->sendData(4);
        }
        else if (Renderarea->is_pressed[0]) {
            sendMove->sendData(3);
            sendMove->sendData(0);
        }
        else if (Renderarea->is_pressed[1]) {
            sendMove->sendData(3);
            sendMove->sendData(1);
        }
        else {
            sendMove->sendData(3);          
        }   
        Renderarea->update();
        qDebug() << test;
    }

    test++;
}
void RCcar::keyReleaseEvent(QKeyEvent* event) {
    qDebug() << "hahaha";
    sendMove->sendData(5);
    if (!event->isAutoRepeat()) {
        sendMove->sendData(4);
        if (event->key() == Qt::Key_A) {
            Renderarea->is_pressed[0] = false;          
            Renderarea->update();
            return;
        }
        if (event->key() == Qt::Key_D) {
            Renderarea->is_pressed[1] = false;
            Renderarea->update();
            return;
        }
        if (event->key() == Qt::Key_S) {
            Renderarea->is_pressed[2] = false;
            Renderarea->update();
        }
        if (event->key() == Qt::Key_W) {
            Renderarea->is_pressed[3] = false;
            Renderarea->update();
            return;
        }

    }   
}

So just to make it perfectly clear what is happening, If I press and hold down w then the console prints the integer test and "hahaha" alternating, if I then press down d while still holding down w, it just prints "hahaha" and I know its calling Key_D from keypressevent, if I then stop pressing d then the console doesn't print anything even though I am still holding down the w button.

thanks

Farhad
  • 4,119
  • 8
  • 43
  • 66
lulz
  • 93
  • 9
  • Why you're not using qt shortcuts ? https://stackoverflow.com/a/45730106/5068056 – Farhad Sep 03 '17 at 10:41
  • 3
    You are implicitly relying on the keyboard controller itself repeating a key when you hold it down. But that stops when you press another key and won't resume without releasing and pressing the key again. It is just not the correct way to implement a WASD style game control, you instead need to use the keydown and keyup events to set a direction variable and have the game loop, or a timer, implement the motion. – Hans Passant Sep 03 '17 at 13:18
  • From the OP, now deleted: "I found this which solves my problem http://www.qtforum.org/article/28368/solved-keyboard-input-for-diagonal-movement-of-game-character.html?s=9d20c205db5763f8ec40c71479855b21e28ecea8" – Hans Passant Sep 17 '17 at 08:47

0 Answers0