Analysis and example of Qt global hotkey QxtGlobalShortcut

Source: Internet
Author: User

QtGlobal hotkeyQxtGlobalShortcutAnalyze and use examples to share with you. If you need a global hotkey, this article will solve it for you.

First, use RegisterHotKey and UnregisterHotKey for registration and anti-registration.Hotkey

Second, in the event global filter winEventFilter, the correspondingHotkeyBut it does not seem to mention much in Chinese documents that involve other platforms.

Libqxt

ActuallyQtThe third-party library libqxt has provided a better cross-platform solution, which isQxtGlobalShortcut.

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 will strip outQxtGlobalShortcutAs the 3 rdparty part of the project.

Let's take a look at this.QxtGlobalShortcut:

Usage

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

 
  1. QxtGlobalShortcut* shortcut = new QxtGlobalShortcut(QKeySequence("Ctrl+Shift+F12"), w); 

Connect (shortcut, SIGNAL (activated (), w, SLOT (myslot (); Note: If you are not interested in its internal implementation, you 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

 
  1. gxtglobal.h  

Libqxt a Global File

 
  1. qxtglobalshortcut.h  

Interface files that we are most concerned about

 
  1. qxtglobalshortcut_.h  

Private header file

 
  1. gxtglobalshortcut.cpp  

Class implementation (platform-independent)

 
  1. gxtglobalshortcut_win.cpp  

Class implementation (win)

 
  1. gxtglobalshortcut_x11.cpp  

Class implementation (x11)

 
  1. 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.

 
  1. bool QxtGlobalShortcutPrivate::setShortcut(const QKeySequence& shortcut)  
  2. {  
  3. ...  
  4.     key = ...  
  5.     mods = ...  
  6.     const quint32 nativeKey = nativeKeycode(key);  
  7.     const quint32 nativeMods = nativeModifiers(mods);  
  8.     const bool res = registerShortcut(nativeKey, nativeMods);  
  9.     shortcuts.insert(qMakePair(nativeKey, nativeMods), &qxt_p()); 

The process of anti-registration is similar.

Hotkey Activation

First install the Event Filter for qiniacteventdispatcher.

 
  1. Qiniacteventdispatcher: instance ()-> setEventFilter (eventFilter); note that QApplication is not installed here. This is what Manual tells us.
  2. Bool QCoreApplication: winEventFilter (MSG * msg, long * result) [virtual]
  3. To handle system wide messages, such as messages from a registered hot key,
  4. You need to install an event filter on the event dispatcher,
  5. Which is returned from qiniacteventdispatcher: instance (). Then we process them separately in the filter, for example, in windows
  6. Bool QxtGlobalShortcutPrivate: eventFilter (void * message)
  7. {
  8. MSG * msg = static_cast <MSG *> (message );
  9. If (msg-> message = WM_HOTKEY ){
  10. Const quint32 keycode = HIWORD (msg-> lParam );
  11. Const quint32 modifiers = LOWORD (msg-> lParam );
  12. ActivateShortcut (keycode, modifiers );
  13. }
  14. Return false;
  15. }
  16. Or under x11:
  17. Bool QxtGlobalShortcutPrivate: eventFilter (void * message)
  18. {
  19. XEvent * event = static_cast <XEvent *> (message );
  20. If (event-> type = KeyPress ){
  21. XKeyEvent * key = (XKeyEvent *) event;
  22. ActivateShortcut (key-> keycode, key-> state & (ShiftMask | ControlMask | Mod1Mask | Mod4Mask ));
  23. }
  24. Return false;
  25. }

ActivateShortcut is platform independent:

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

Remember to registerHotkeyIs it to save things to a QHash? It works here, whenHotkeyAt the time of arrival, we can find the corresponding QHash.QxtGlobalShortcutYes. If yes and enabled, the System emits a signal.

How to Use

For ease of use, we can addQxtglobalshortcut. Pri file:

 
  1. INCLUDEPATH += $$PWD  
  2. DEPENDPATH += $$PWD  
  3.  
  4. HEADERS += $$PWD/qxtglobal.h \  
  5.            $$PWD/qxtglobalshortcut.h \  
  6.            $$PWD/qxtglobalshortcut_p.h  
  7. SOURCES += $$PWD/qxtglobalshortcut.cpp  
  8. win32{  
  9.     SOURCES += $$PWD/qxtglobalshortcut_win.cpp  
  10.     LIBS += -luser32  
  11. }  
  12. unix:SOURCES += $$PWD/qxtglobalshortcut_x11.cpp  
  13. mac: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 ).

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

Summary:QtGlobalHotkey QxtGlobalShortcutThe Analysis and examples are provided to help you solve the problem.

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.