Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using multiple keys in Qt C++

Tags:

c++

qt

I'm building the "Tanks" game where I'm using Key event to run my tank on map. Actually I can only use one key at the time but I need to have ability to f.e. go up and left simultaneously.

Here's my code for single key events:

switch(event->key())
{
case Qt::Key_Up:
    if(!ui->widget->playerList[playerID]->canMove(0.3, 20, 20, -20, -20, 1.5)) return;
    ui->widget->playerList[playerID]->move(0.3);
    ui->widget->updateGL();
    break;
case Qt::Key_Down:
    if(!ui->widget->playerList[playerID]->canMove(-0.2, 20, 20, -20, -20, 1.5)) return;
    ui->widget->playerList[playerID]->move(-0.2);
    ui->widget->updateGL();
    break;
case Qt::Key_Right:
    ui->widget->playerList[playerID]->rotate(10);
    ui->widget->updateGL();
    break;
case Qt::Key_Left:
    ui->widget->playerList[playerID]->rotate(-10);
    ui->widget->updateGL();
    break;
case Qt::Key_Q:
    ui->widget->playerList[playerID]->rotateCannon(10);
    ui->widget->updateGL();
    break;
case Qt::Key_E:
    ui->widget->playerList[playerID]->rotateCannon(-10);
    ui->widget->updateGL();
    break;
default:
    QMainWindow::keyPressEvent(event);
    break;
} 
like image 741
Tomasz Kasperek Avatar asked Mar 20 '23 13:03

Tomasz Kasperek


2 Answers

You can add a pressed key to the set of pressed keys and remove from this set when the key is released. So you can add the pressed key to a QSet which is a class member :

QSet<int> pressedKeys;

You can catch the key events in an event filter :

bool MyWidget::eventFilter(QObject * obj, QEvent * event)
{

    if(event->type()==QEvent::KeyPress) {

        pressedKeys += ((QKeyEvent*)event)->key();

        f( pressedKeys.contains(Qt::Key_Up) && pressedKeys.contains(Qt::Key_Left) )
        {
            // up and left is pressed
        }

    }
    else if(event->type()==QEvent::KeyRelease)
    {

        pressedKeys -= ((QKeyEvent*)event)->key();
    }


    return false;
}

Don't forget to install the event filter in the constructor:

this->installEventFilter(this);
like image 192
Nejat Avatar answered Mar 28 '23 02:03

Nejat


Maybe you could consider looking into the masks? Let's say you have directions, define a mask that has four bits:

0 0 0 0

U D L R

And each time you just need to check what keys are pressed. By AND with the masks:

1000 - Up

0111 - Down

0010 - Left

0001 - Right

like image 26
Juto Avatar answered Mar 28 '23 03:03

Juto