如何检测全局键序列在Qt中按? [英] How to detect global key sequence press in Qt?

查看:125
本文介绍了如何检测全局键序列在Qt中按?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检测一个键序列是否被按下,并想在Qt上对该事件执行某些任务。目前,我可以检测某些小部件的按键,但如何检测全局按键。全局我的意思是,即使应用程序被最小化或隐藏,它应该检测按键。

I want to detect whether a key sequence is pressed and want to perform certain task on that event in Qt. Currently I can detect keypresses for certain widgets but how to detect global keypresses. By global I mean that even if the application is minimized or hidden , it should detect key press.

我试图做一个 eventFilter 为应用程序,通过首先重载 QObject :: eventFilter 像这样:

I tried making an eventFilter for the application, by first overloading QObject::eventFilter like this:

bool GlobalEventFilter::eventFilter(QObject *Object, QEvent *Event)
{
  if (Event->type() == QEvent::KeyPress)
  {
    QKeyEvent *KeyEvent = (QKeyEvent*)Event;

    switch(KeyEvent->key())
    {
      case Qt::Key_F1:
        cout<<"F1 press detected"<<endl;
        return true;
      default:
        break;
    }
  }
  return QObject::eventFilter(Object,Event);
}

,然后将该对象安装为 eventFilter 为我的应用程序:

and then installing that object as the eventFilter for my application:

QApplication a(argc,argv);
a.installEventFilter(new GlobalEventFilter());

我也尝试过这样做:

QCoreApplication::instance()->installEventFilter(new GlobalEventFilter());

在这两种情况下,我可以在应用程序窗口打开时检测按键,窗口最小化或隐藏。如何解决这个问题?

In both the cases I am able to detect key presses when my application window is open but it fails when window is minimized or hidden. How to solve this?

推荐答案

请参阅 QKeyEvent


键事件发送到

Key events are sent to the widget with keyboard input focus when keys are pressed or released.

这意味着,如果你想使用 QKeyEvent ,你需要有键盘焦点。过滤这些事件在概念上也不会改变。我不知道你从哪里得到过滤的想法。

That means, if you wish to use QKeyEvent, you need to have the keyboard focus. Filtering those events does not change this conceptually either. I am not sure where you got the filtering idea from.

你可能想研究替代解决方案,例如它是如何在kwin等实现。一般来说,请小心这个用例。

You might want to look into alternative solutions, for instance how it is implemented in kwin, etc. In general, be careful with this use case. It could do weird things without the end users noticing it.

你可以看看这个类:

QxtGlobalShortcut类参考

您可以这样写:

#include <QxtGlobalShortcut>

#include <QApplication>
#include <QMainWindow>
#include <QDebug>

class MyGlobalShortcutHandler : public QObject
{
    Q_OBJECT
public:
    explicit MyGlobalShortcutHandler(QObject *parent = 0)
        : QObject(parent)
    {
        m_globalShortcut.setShortcut(QKeySequence("Ctrl+Shift+X"));
        m_globalShortcut.setEnabled(true);

        connect(&m_globalShortcut, SIGNAL(activated()), SLOT(handleGlobalShortcut()));
    }

public slots:
    void handleGlobalShortcut()
    {
        qDebug() << "Global shortcut handled";
    }

private:
    QxtGlobalShortcut m_globalShortcut;
};

#include "main.moc"

int main(int argc, char **argv)
{
    QApplication application(argc, argv);
    QMainWindow mainWindow;
    MyGlobalShortcutHandler myGlobalShortcut();
    mainWindow.show();
    return application.exec();
}



main.pro



main.pro

TEMPLATE = app
TARGET = main
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += qxt
QXT = core gui
SOURCES += main.cpp



构建并运行



Build and Run

qmake-qt4 && make && ./main

此代码至少应该与Qt 4一起使用。它为我打印出debug语句。

This code should be working with Qt 4, at least. It prints out the debug statement for me. You could easily verify this on your end.

要获取libqxt库,请访问以下链接:

To get the libqxt library, please visit the following link:

http://dev.libqxt.org/libqxt/wiki/Home

这篇关于如何检测全局键序列在Qt中按?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆