QT/c++ 如何判断全局是鼠标按下还是松开?

QT/c++ How to check globaly is mouse press or release?

void MainWindow::mousePressEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton)
    {
      timer->start(timer_time);
    }
}

void MainWindow::mouseReleaseEvent(QMouseEvent* event)
{
    if(event->button() == Qt::LeftButton)
    {
        timer->stop();
    }
}

此代码在我的应用程序内部使用时有效,但如果我想在外部使用它,它将无法正常工作。我怎样才能做到这一点? 它应该在左键按下时启动计时器,在左键释放时停止。


解决方案: understanding-the-low-level-mouse-and-keyboard-hook-win32


听起来 hooks 就是您要找的东西。

基本上,挂钩可以让您与系统中的 activity 交互,并且可以用来改变它的行为。

代码中的事件侦听器只会捕获 OS 委托给 QT 应用程序的鼠标事件。这就是为什么您的代码仅在您在应用程序中使用时才有效。使用挂钩,您可以在系统级别拦截鼠标事件并让它们在您的应用程序中处理它们,而不是让 OS 决定它们应该在哪里处理。

Here's what to use to set them up, and here关于实施细节的小指南。