Qt多键持有KeyReleaseEvent,KeyPressEvent不会触发

时间:2017-09-03 10:40:01

标签: c++ qt

我有一个程序,例如,如果我按住W然后D一切正常,但是如果我然后释放D似乎KeyPressEvent或KeyReleaseEvent不触发w,我已经使用qDebug检查了这个,这发生在任何序列按键,只要按上面指定的顺序按下和按下即可。但似乎Renderarea仍然被称为......

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;
        }

    }   
}

所以只是为了清楚地说明发生了什么,如果我按住w然后控制台打印整数测试和&#34; haha​​ha&#34;交替,如果我按下d,同时仍然按住w,它只是打印&#34;哈哈哈&#34;我知道它从keypressevent调用Key_D,如果我再停止按d,那么即使我仍然按住w按钮,控制台也不会打印任何内容。

感谢

0 个答案:

没有答案