diff --git a/src/qt/qt_main.cpp b/src/qt/qt_main.cpp index f011fd9f7..40d8680eb 100644 --- a/src/qt/qt_main.cpp +++ b/src/qt/qt_main.cpp @@ -94,6 +94,14 @@ int main(int argc, char* argv[]) { main_window = new MainWindow(); main_window->show(); + main_window->setFocus(); + app.installEventFilter(main_window); + auto widgetList = app.allWidgets(); + for (auto curWidget : widgetList) + { + curWidget->setFocusPolicy(Qt::NoFocus); + } + main_window->setFocusPolicy(Qt::StrongFocus); pc_init(argc, argv); if (! pc_init_modules()) { diff --git a/src/qt/qt_mainwindow.cpp b/src/qt/qt_mainwindow.cpp index 6bc350908..3b1957c64 100644 --- a/src/qt/qt_mainwindow.cpp +++ b/src/qt/qt_mainwindow.cpp @@ -13,11 +13,11 @@ extern "C" { }; #include -#include #include #include #include #include +#include #include @@ -28,6 +28,8 @@ extern "C" { #ifdef __unix__ #include #include +#undef KeyPress +#undef KeyRelease #endif extern void qt_mouse_capture(int); @@ -94,6 +96,16 @@ MainWindow::MainWindow(QWidget *parent) : ui->actionKeyboard_requires_capture->setChecked(kbd_req_capture); ui->actionRight_CTRL_is_left_ALT->setChecked(rctrl_is_lalt); + setFocusPolicy(Qt::StrongFocus); + ui->gles->setFocusPolicy(Qt::NoFocus); + ui->sw->setFocusPolicy(Qt::NoFocus); + ui->ogl->setFocusPolicy(Qt::NoFocus); + ui->stackedWidget->setFocusPolicy(Qt::NoFocus); + ui->centralwidget->setFocusPolicy(Qt::NoFocus); + menuBar()->setFocusPolicy(Qt::NoFocus); + menuWidget()->setFocusPolicy(Qt::NoFocus); + statusBar()->setFocusPolicy(Qt::NoFocus); + video_setblit(qt_blit); } @@ -683,6 +695,23 @@ void MainWindow::getTitle(wchar_t *title) } } +bool MainWindow::eventFilter(QObject* receiver, QEvent* event) +{ + if (this->keyboardGrabber() == this) { + if (event->type() == QEvent::KeyPress) { + event->accept(); + this->keyPressEvent((QKeyEvent*)event); + return true; + } + if (event->type() == QEvent::KeyRelease) { + event->accept(); + this->keyReleaseEvent((QKeyEvent*)event); + return true; + } + } + return QMainWindow::eventFilter(receiver, event); +} + void MainWindow::refreshMediaMenu() { mm->refresh(ui->menuMedia); } @@ -715,6 +744,7 @@ void MainWindow::keyPressEvent(QKeyEvent* event) if (keyboard_ismsexit()) { plat_mouse_capture(0); } + event->accept(); } void MainWindow::blitToWidget(int x, int y, int w, int h) @@ -742,3 +772,13 @@ void MainWindow::on_actionHardware_Renderer_OpenGL_triggered() { void MainWindow::on_actionHardware_Renderer_OpenGL_ES_triggered() { ui->stackedWidget->setCurrentIndex(2); } + +void MainWindow::focusInEvent(QFocusEvent* event) +{ + this->grabKeyboard(); +} + +void MainWindow::focusOutEvent(QFocusEvent* event) +{ + this->releaseKeyboard(); +} \ No newline at end of file diff --git a/src/qt/qt_mainwindow.hpp b/src/qt/qt_mainwindow.hpp index d59931cbd..6c56cfd6a 100644 --- a/src/qt/qt_mainwindow.hpp +++ b/src/qt/qt_mainwindow.hpp @@ -4,6 +4,7 @@ #include #include #include +#include #include @@ -63,6 +64,9 @@ private slots: protected: void keyPressEvent(QKeyEvent* event) override; void keyReleaseEvent(QKeyEvent* event) override; + void focusInEvent(QFocusEvent* event) override; + void focusOutEvent(QFocusEvent* event) override; + bool eventFilter(QObject* receiver, QEvent* event) override; private: Ui::MainWindow *ui;