diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index 82f9acd5..def901ed 100644 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -64,6 +64,18 @@ MainWindow::MainWindow(QWidget *parent) { setAttribute(Qt::WA_TranslucentBackground); setWindowFlags(Qt::X11BypassWindowManagerHint); + + installEventFilter(this); + + connect(this, &MainWindow::releaseEvent, this, [=]{ + qDebug() << "release event !!!"; + m_keyboardReleased = true; + m_keyboardGrabbed = windowHandle()->setKeyboardGrabEnabled(false); + qDebug() << "keyboardGrabbed:" << m_keyboardGrabbed; + removeEventFilter(this); + }); + + connect(this, &MainWindow::hideScreenshotUI, this, &MainWindow::hide); } MainWindow::~MainWindow() @@ -516,19 +528,22 @@ void MainWindow::mouseReleaseEvent(QMouseEvent *ev) QLabel::mouseReleaseEvent(ev); } -void MainWindow::showEvent(QShowEvent *event) -{ - QTimer::singleShot(100, this, &MainWindow::grabKeyboard); - - return QLabel::showEvent(event); -} - void MainWindow::hideEvent(QHideEvent *event) { qApp->setOverrideCursor(Qt::ArrowCursor); QLabel::hideEvent(event); } +bool MainWindow::eventFilter(QObject *watched, QEvent *event) +{ + if (!m_keyboardGrabbed && this->windowHandle() != NULL) { + m_keyboardGrabbed = this->windowHandle()->setKeyboardGrabEnabled(true); + qDebug() << "m_keyboardGrabbed:" << m_keyboardGrabbed; + } + + return QLabel::eventFilter(watched, event); +} + void MainWindow::mouseMoveEvent(QMouseEvent *ev) { bool needRepaint = false; diff --git a/src/mainwindow.h b/src/mainwindow.h index 05c95f51..d27eadee 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -118,8 +118,8 @@ public slots: void mousePressEvent(QMouseEvent* ev) Q_DECL_OVERRIDE; void mouseMoveEvent(QMouseEvent *ev) Q_DECL_OVERRIDE; void mouseReleaseEvent(QMouseEvent *ev) Q_DECL_OVERRIDE; - void showEvent(QShowEvent *event) override; void hideEvent(QHideEvent *event) Q_DECL_OVERRIDE; + bool eventFilter(QObject* watched, QEvent* event) override; private: QPixmap getPixmapofRect(const QRect &rect); @@ -161,6 +161,8 @@ public slots: bool m_isShiftPressed = false; bool m_noNotify = false; bool m_needSaveScreenshot = false; + bool m_keyboardGrabbed = false; + bool m_keyboardReleased = false; QString m_selectAreaName; QPixmap m_resizeBigPix; diff --git a/src/screenshot.cpp b/src/screenshot.cpp index 46bd1c47..4ced4276 100644 --- a/src/screenshot.cpp +++ b/src/screenshot.cpp @@ -38,17 +38,6 @@ Screenshot::Screenshot(QObject *parent) void Screenshot::initUI() { m_eventContainer = new EventContainer(this); m_window = new MainWindow; - - connect(m_window, &MainWindow::releaseEvent, this, [=]{ - qDebug() << "release event !!!"; - m_keyboardReleased = true; - m_keyboardGrabbed = m_window->windowHandle()->setKeyboardGrabEnabled(false); - qDebug() << "keyboardGrabbed:" << m_keyboardGrabbed; - removeEventFilter(this); - }); - connect(m_window, &MainWindow::hideScreenshotUI, this, [=]{ - m_window->hide(); - }); } void Screenshot::startScreenshot() @@ -108,4 +97,3 @@ void Screenshot::savePathScreenshot(const QString &path) } Screenshot::~Screenshot() {} - diff --git a/src/screenshot.h b/src/screenshot.h index 8325c806..76d3e729 100644 --- a/src/screenshot.h +++ b/src/screenshot.h @@ -41,9 +41,6 @@ public slots: void initUI(); EventContainer* m_eventContainer = nullptr; - bool m_keyboardGrabbed = false; - bool m_keyboardReleased = false; - MainWindow* m_window = nullptr; };