Analysis and use of QT global hotkey qxtglobalshortcut

Source: Internet
Author: User
Tags one more line


Many people need to add global hotkeys to the program. You can find a lot of methods to introduce how to implement windows by searching on the Internet:

  • First, use registerhotkey and unregisterhotkey to register and unregister hotkeys.

  • Second, the corresponding hotkey In the event global filter wineventfilter

However, it does not seem to mention much in Chinese documents that involve other platforms.

Libqxt

In fact, the third-party library libqxt of QT has provided a better cross-platform solution, which is qxtglobalshortcut.

Therefore, you can use this third-party library.

However, do you think it is a little difficult to introduce this libqxt third-party library just to use this class? Can I strip it out for separate use?

Qxtglobalshortcut

Well, you can find out by Google that many people have done this job on the Internet. You will find that many people use the separated qxtglobalshortcut as the 3 rdparty part of the project.

Then let's take a look at the qxtglobalshortcut:

Usage

It's so simple that it's no longer easy, right?

QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(QKeySequence("Ctrl+Shift+F12"), w);connect(shortcut, SIGNAL(activated()), w, SLOT(myslot()));

Note: If you are not interested in the internal implementation and only care about how to use it, this small example may be all you need.
: Http://code.google.com/p/h-qt-exercise/downloads/detail? Name‑shortcut.zip & can = 2 & Q =

Source code file
 

Gxtglobal. h

Libqxt a Global File

*

Qxtglobalshortcut. h

Interface files that we are most concerned about

 

Qxtglobalshortcut _. h

Private header file

 

Gxtglobalshortcut. cpp

Class implementation (platform-independent)

 

Gxtglobalshortcut_win.cpp

Class implementation (WIN)

 

Gxtglobalshortcut_x11.cpp

Class implementation (X11)

 

Gxtglobalshortcut_mac.cpp

Class implementation (MAC)

Registration Process of hotkeys

When we set a hot key through the constructor or the setshortcut member:

  • First, the qkeysequence is divided into two parts: QT: Key and QT: keyboardmodifiers.

    • (That is, the following key and mod)
  • Then, the key and MoD are converted into the platform-related (native) code values, and the platform-related functions are called for registration.
  • Finally, the qpair consisting of the key and the native code value of the MOD is used as the qhash key, and the pointer of the shortcut is saved as the value.

 

bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut){...    key = ...    mods = ...    const quint32 nativeKey = nativeKeycode(key);    const quint32 nativeMods = nativeModifiers(mods);    const bool res = registerShortcut(nativeKey, nativeMods);    shortcuts.insert(qMakePair(nativeKey, nativeMods), &qxt_p());...
  • The process of anti-registration is similar.
Hotkey Activation
  • First install the Event Filter for qiniacteventdispatcher.

 

QAbstractEventDispatcher::instance()->setEventFilter(eventFilter);

Note that qapplication is not installed here. This is what manual tells us.

bool QCoreApplication::winEventFilter ( MSG * msg, long * result ) [virtual]To handle system wide messages, such as messages from a registered hot key, you need to install an event filter on the event dispatcher, which is returned from QAbstractEventDispatcher::instance().
  • Then we process the filters separately, for example, in Windows

 

bool QxtGlobalShortcutPrivate::eventFilter(void* message){    MSG* msg = static_cast<MSG*>(message);    if (msg->message == WM_HOTKEY){        const quint32 keycode = HIWORD(msg->lParam);        const quint32 modifiers = LOWORD(msg->lParam);        activateShortcut(keycode, modifiers);    }    return false;}

Or under X11:

bool QxtGlobalShortcutPrivate::eventFilter(void* message){    XEvent* event = static_cast<XEvent*>(message);    if (event->type == KeyPress){        XKeyEvent* key = (XKeyEvent*) event;        activateShortcut(key->keycode, key->state & (ShiftMask | ControlMask | Mod1Mask | Mod4Mask));    }    return false;}

Activateshortcut is platform independent:

void QxtGlobalShortcutPrivate::activateShortcut(quint32 nativeKey, quint32 nativeMods){    QxtGlobalShortcut* shortcut = shortcuts.value(qMakePair(nativeKey, nativeMods));    if (shortcut && shortcut->isEnabled())        emit shortcut->activated();}

Do you remember that the previous registration hotkey saved something to a qhash? It works here. When a hot key arrives, we can find from this qhash whether there is a corresponding qxtglobalshortcut. If it exists and is enabled, it will send a signal.

How to Use

For ease of use, we can add a qxtglobalshortcut. PRI file in the directory at the same level of this file:

INCLUDEPATH += $$PWDDEPENDPATH += $$PWDHEADERS += $$PWD/qxtglobal.h /           $$PWD/qxtglobalshortcut.h /           $$PWD/qxtglobalshortcut_p.hSOURCES += $$PWD/qxtglobalshortcut.cppwin32{    SOURCES += $$PWD/qxtglobalshortcut_win.cpp    LIBS += -luser32}unix:SOURCES += $$PWD/qxtglobalshortcut_x11.cppmac:SOURCES += $$PWD/qxtglobalshortcut_mac.cpp

In this way, when we use it, we only need to add one more line in our pro file:

include(yourpath/qxtglobalshortcut.pri)

You can.

A small example: http://code.google.com/p/h-qt-exercise/downloads/detail? Name‑shortcut.zip & can = 2 & Q =

Reference
  • Http://libqxt.bitbucket.org/doc/tip/qxtglobalshortcut.html

  • Http://software.intel.com/zh-cn/articles/jason_lee20100430/

  • Http://www.cuteqt.com/blog? P = 2088

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.