我正在使用Qt为我的游戏构建一个简单的关卡编辑器。要导航3d场景,我需要同时使用键盘和鼠标。
这是我到目前为止的工作,但是当我使用WASD移动时,我无法环顾四周。如何同时捕获和响应键盘和鼠标事件?
void MainWindow::mousePressEvent(QMouseEvent* event)
{
if(event->buttons() == Qt::LeftButton && actionCamera)
{
ui->renderWindow->trackMouse = true;
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent* event)
{
ui->renderWindow->trackMouse = false;
}
void MainWindow::keyPressEvent(QKeyEvent* event)
{
if(event->key() == Qt::Key_W)
{
ui->renderWindow->moveForward = true;
}
if(event->key() == Qt::Key_S)
{
ui->renderWindow->moveBackwards = true;
}
if(event->key() == Qt::Key_A)
{
ui->renderWindow->moveLeft = true;
}
if(event->key() == Qt::Key_D)
{
ui->renderWindow->moveRight = true;
}
}
void MainWindow::keyReleaseEvent(QKeyEvent* event)
{
if(event->key() == Qt::Key_W)
{
ui->renderWindow->moveForward = false;
}
if(event->key() == Qt::Key_S)
{
ui->renderWindow->moveBackwards = false;
}
if(event->key() == Qt::Key_A)
{
ui->renderWindow->moveLeft = false;
}
if(event->key() == Qt::Key_D)
{
ui->renderWindow->moveRight = false;
}
}